我在VC中用ADO调用存储过程,出错,帮忙看看吧 http://www.csdn.net/expert/topic/590/590568.xml?temp=.3345148 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一个完整的例子,希望对你有帮助//根据你的配置而改变#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile")#include <ole2.h>#include <stdio.h>#include <conio.h>#include "ActiveCommandX.h"// Function declarationsinline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};void ActiveCommandX(void);void ActiveCommandXprint(_RecordsetPtr pRst);void PrintProviderError(_ConnectionPtr pConnection);void PrintComError(_com_error &e);//////////////////////////////////////////////////////////// //// Main Function //// ////////////////////////////////////////////////////////////void main(){ if(FAILED(::CoInitialize(NULL))) return; ActiveCommandX(); ::CoUninitialize();}//////////////////////////////////////////////////////////// //// ActiveCommandX Function //// ////////////////////////////////////////////////////////////void ActiveCommandX(void) { HRESULT hr = S_OK; // Define ADO object pointers. // Initialize pointers on define. // These are in the ADODB:: namespace. _ConnectionPtr pConnection = NULL; _CommandPtr pCmd = NULL; _RecordsetPtr pRstAuthors = NULL; //Definitions of other variables //根据你的配置而改变,需要sql server _bstr_t strCnn("Provider=SQLOLEDB;Data Source=srv;” “Initial Catalog=Pubs;User Id=sa;Password=;"); _bstr_t strName; CHAR strcharName[50]; try { // Open connection. TESTHR(pConnection.CreateInstance(__uuidof(Connection))); TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset))); TESTHR(pCmd.CreateInstance(__uuidof(Command))); printf("ActiveCommandX Example\n\n"); printf("Enter an author's name (e.g., Ringer): "); gets(strcharName); char *tempStr = strtok(strcharName, " \t"); strName = tempStr; pCmd->CommandText = "SELECT * FROM authors WHERE au_lname = ?"; pCmd->Parameters->Append(pCmd->CreateParameter("LastName", adChar, adParamInput, 20, strName)); pConnection->Open (strCnn, "", "", NULL); pCmd->PutActiveConnection(_variant_t((IDispatch*)pConnection)); pRstAuthors = pCmd->Execute(NULL,NULL,adCmdText); ActiveCommandXprint(pRstAuthors); // Clean up objects before exit. pRstAuthors->Close(); pConnection->Close(); } catch(_com_error &e) { // Notify the user of errors if any. // Pass a connection pointer accessed from the Recordset. PrintProviderError(pConnection); PrintComError(e); }}//////////////////////////////////////////////////////////// //// ActiveCommandXprint Function //// ////////////////////////////////////////////////////////////void ActiveCommandXprint(_RecordsetPtr pRst = NULL){ // Varible declaraion & initialization IADORecordBinding *picRs = NULL; //Interface Pointer declared. CAuthorsRs autrs; //C++ class object HRESULT hr; bstr_t strName; //Open an IADORecordBinding interface pointer which //we'll use for Binding Recordset to a class. TESTHR(pRst->QueryInterface( __uuidof(IADORecordBinding),(LPVOID*)&picRs)); //Bind the Recordset to a C++ Class here. TESTHR(picRs->BindToRecordset(&autrs)); strName = ((_CommandPtr)pRst->GetActiveCommand())-> GetParameters()->GetItem("LastName")->Value; printf("Command text = '%s'\n", (LPCSTR)((_CommandPtr)pRst->GetActiveCommand())->CommandText); printf("Parameter = '%s'\n", (LPCSTR)strName); if (pRst->BOF) printf("Name = '%s'not found.", (LPCSTR)strName); else { printf ("Name = '%s %s' author ID = '%s'", autrs.lau_fnameStatus == adFldOK ? autrs.m_au_fname : "<NULL>", autrs.lau_lnameStatus == adFldOK ? autrs.m_au_lname : "<NULL>", autrs.lau_idStatus == adFldOK ? autrs.m_au_id : "<NULL>"); } //Release IADORecordset Interface if (picRs) picRs->Release();}//////////////////////////////////////////////////////////// //// PrintProviderError Function //// ////////////////////////////////////////////////////////////void PrintProviderError(_ConnectionPtr pConnection){ // Print Provider Errors from Connection object. // pErr is a record object in the Connection's Error collection. ErrorPtr pErr = NULL; long nCount = 0; long i = 0; if( (pConnection->Errors->Count) > 0) { nCount = pConnection->Errors->Count; // Collection ranges from 0 to nCount -1. for(i = 0; i < nCount; i++) { pErr = pConnection->Errors->GetItem(i); printf("Error number: %x\t%s\n", pErr->Number, (LPCSTR)pErr->Description); } }}//////////////////////////////////////////////////////////// //// PrintComError Function //// ////////////////////////////////////////////////////////////void PrintComError(_com_error &e){ _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); // Print Com errors. printf("\nError\n"); printf("\tCode = %08lx\n", e.Error()); printf("\tCode meaning = %s\n", e.ErrorMessage()); printf("\tSource = %s\n", (LPCSTR) bstrSource); printf("\tDescription = %s\n", (LPCSTR) bstrDescription);}ActiveCommandX.h#include "icrsint.h"// This Class extracts id, fname, lname from authors table.class CAuthorsRs : public CADORecordBinding{BEGIN_ADO_BINDING(CAuthorsRs) // Column au_id is the 1st field in the recordset ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_au_id, sizeof(m_au_id), lau_idStatus, TRUE) // Column au_fname is the 2nd field in the recordset ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_au_fname, sizeof(m_au_fname), lau_fnameStatus, TRUE) // Column au_lname is the 3rd field in the recordset ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_au_lname, sizeof(m_au_lname), lau_lnameStatus, TRUE)END_ADO_BINDING()public: char m_au_id[21]; ULONG lau_idStatus; char m_au_fname[41]; ULONG lau_fnameStatus; char m_au_lname[41]; ULONG lau_lnameStatus;}; socks5代理问题。。。 MFC用不用学啊,咱们老师说不用学了。 怎样给图片加上坐标? 关于这种的Ascii码16进制字符串怎么转换? 求助:关于对话框的显示与销毁问题。谢谢各位大侠。(续集) 如何让对话框初始时不可见? 请问如何去掉editbox的默认的edit文字? 如何写程序记录oicq的密码和用户名 小生,问各位高手:在“ATAPI CD-ROM MAX 52X”中,ATAPI代表什么意思? 如何让应用程序生成像任务栏右边时钟一样的托盘文字 怎样在VC的添加虚拟函数或消息函数向导对话框中加入自己的虚函数和自定义函数 100分求自画标题条源程序。
//根据你的配置而改变
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")#include <ole2.h>
#include <stdio.h>
#include <conio.h>
#include "ActiveCommandX.h"// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ActiveCommandX(void);
void ActiveCommandXprint(_RecordsetPtr pRst);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);//////////////////////////////////////////////////////////
// //
// Main Function //
// //
//////////////////////////////////////////////////////////void main()
{
if(FAILED(::CoInitialize(NULL)))
return; ActiveCommandX(); ::CoUninitialize();
}//////////////////////////////////////////////////////////
// //
// ActiveCommandX Function //
// //
//////////////////////////////////////////////////////////void ActiveCommandX(void)
{
HRESULT hr = S_OK; // Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_CommandPtr pCmd = NULL;
_RecordsetPtr pRstAuthors = NULL; //Definitions of other variables
//根据你的配置而改变,需要sql server
_bstr_t strCnn("Provider=SQLOLEDB;Data Source=srv;”
“Initial Catalog=Pubs;User Id=sa;Password=;");
_bstr_t strName;
CHAR strcharName[50]; try
{
// Open connection.
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
TESTHR(pCmd.CreateInstance(__uuidof(Command))); printf("ActiveCommandX Example\n\n");
printf("Enter an author's name (e.g., Ringer): ");
gets(strcharName);
char *tempStr = strtok(strcharName, " \t");
strName = tempStr; pCmd->CommandText = "SELECT * FROM authors WHERE au_lname = ?";
pCmd->Parameters->Append(pCmd->CreateParameter("LastName",
adChar, adParamInput, 20, strName)); pConnection->Open (strCnn, "", "", NULL); pCmd->PutActiveConnection(_variant_t((IDispatch*)pConnection)); pRstAuthors = pCmd->Execute(NULL,NULL,adCmdText); ActiveCommandXprint(pRstAuthors); // Clean up objects before exit.
pRstAuthors->Close();
pConnection->Close();
} catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
PrintProviderError(pConnection);
PrintComError(e);
}
}//////////////////////////////////////////////////////////
// //
// ActiveCommandXprint Function //
// //
//////////////////////////////////////////////////////////void ActiveCommandXprint(_RecordsetPtr pRst = NULL)
{
// Varible declaraion & initialization
IADORecordBinding *picRs = NULL; //Interface Pointer declared.
CAuthorsRs autrs; //C++ class object
HRESULT hr;
bstr_t strName;
//Open an IADORecordBinding interface pointer which
//we'll use for Binding Recordset to a class.
TESTHR(pRst->QueryInterface(
__uuidof(IADORecordBinding),(LPVOID*)&picRs)); //Bind the Recordset to a C++ Class here.
TESTHR(picRs->BindToRecordset(&autrs)); strName = ((_CommandPtr)pRst->GetActiveCommand())->
GetParameters()->GetItem("LastName")->Value; printf("Command text = '%s'\n",
(LPCSTR)((_CommandPtr)pRst->GetActiveCommand())->CommandText); printf("Parameter = '%s'\n", (LPCSTR)strName); if (pRst->BOF)
printf("Name = '%s'not found.", (LPCSTR)strName);
else
{
printf ("Name = '%s %s' author ID = '%s'",
autrs.lau_fnameStatus == adFldOK ? autrs.m_au_fname : "<NULL>",
autrs.lau_lnameStatus == adFldOK ? autrs.m_au_lname : "<NULL>",
autrs.lau_idStatus == adFldOK ? autrs.m_au_id : "<NULL>");
} //Release IADORecordset Interface
if (picRs)
picRs->Release();
}//////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
//////////////////////////////////////////////////////////void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0; if( (pConnection->Errors->Count) > 0)
{
nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n",
pErr->Number, (LPCSTR)pErr->Description);
}
}
}//////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
//////////////////////////////////////////////////////////void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description()); // Print Com errors.
printf("\nError\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}ActiveCommandX.h#include "icrsint.h"// This Class extracts id, fname, lname from authors table.class CAuthorsRs : public CADORecordBinding
{
BEGIN_ADO_BINDING(CAuthorsRs) // Column au_id is the 1st field in the recordset
ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_au_id,
sizeof(m_au_id), lau_idStatus, TRUE) // Column au_fname is the 2nd field in the recordset
ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_au_fname,
sizeof(m_au_fname), lau_fnameStatus, TRUE) // Column au_lname is the 3rd field in the recordset
ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_au_lname,
sizeof(m_au_lname), lau_lnameStatus, TRUE)END_ADO_BINDING()public: char m_au_id[21];
ULONG lau_idStatus;
char m_au_fname[41];
ULONG lau_fnameStatus;
char m_au_lname[41];
ULONG lau_lnameStatus;
};