// SQL Server, stored procedure , Command , Recordset, input parameter, output parameter, returned recordset from stored procedure //execute stored procedure , get returned value, output value, and returned recordset. #include "stdafx.h" #import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" rename( "EOF", "adoEOF" ) struct InitOle { InitOle() { ::CoInitialize(NULL); } ~InitOle() { ::CoUninitialize(); } } _init_InitOle_; void PrintProviderError(ADODB::_ConnectionPtr pConnection);int main(int argc, char* argv[]) { printf("Create a adParamReturnValue for Command object to get return value of stored procedure\n"); printf("Reference :MSDN---Append and CreateParameter Methods Example (VC++)\n"); ADODB::_ConnectionPtr Conn1; ADODB::_CommandPtr Cmd1; ADODB::_RecordsetPtr Rs1; ADODB::_ParameterPtr retParam= NULL; ADODB::_ParameterPtr inParam=NULL; ADODB::_ParameterPtr outParam=NULL; _variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR); _variant_t vtEmpty2 (DISP_E_PARAMNOTFOUND, VT_ERROR); _bstr_t bstrConnect( L"driver={sql server};server=Cell;Database=zhg;UID=sa;PWD=;" ); // the following stored procedure return 12345 int value _bstr_t bstrCreate ( L"create proc sp_AdoTest( @OutParam int OUTPUT,@InParam int ) " L"as " L"select @OutParam = @InParam + 10 " L"select * from Table1" L"return 12345" ); _bstr_t bstrSP(L"sp_Adotest" ); try { _bstr_t bstrEmpty; Conn1.CreateInstance( __uuidof( ADODB::Connection ) ); Cmd1.CreateInstance( __uuidof( ADODB::Command ) ); Rs1.CreateInstance(__uuidof(ADODB::Recordset)); // Establish connection. Conn1->ConnectionString = bstrConnect; Conn1->Open( bstrConnect, bstrEmpty, bstrEmpty, -1 ); // Open recordset. Cmd1->ActiveConnection = Conn1; Cmd1->CommandText = bstrSP; Cmd1->CommandType = ADODB::adCmdStoredProc; retParam=Cmd1->CreateParameter(_bstr_t("Return"),ADODB::adInteger,ADODB::adParamReturnValue,sizeof(int)); Cmd1->Parameters->Append(retParam); outParam = Cmd1->CreateParameter(_bstr_t("OutParam"),ADODB::adInteger,ADODB::adParamOutput,sizeof(int)); Cmd1->Parameters->Append(outParam); inParam = Cmd1->CreateParameter(_bstr_t("InParam"),ADODB::adInteger,ADODB::adParamInput,sizeof(int),_variant_t( (long) 10 )); inParam->Value=_variant_t( (long) 10 ); Cmd1->Parameters->Append(inParam); Cmd1->Parameters->Refresh(); Cmd1->Parameters->Item[ _variant_t( _bstr_t("@InParam") ) ]->Value =_variant_t( (long) 11 ); Rs1->put_CursorLocation(ADODB::adUseClient); Rs1->Open((_variant_t((IDispatch *) Cmd1)),vtEmpty,ADODB::adOpenStatic, ADODB::adLockReadOnly, -1); // Get return value of the stored procedure.adCmdUnknown TCHAR tcbuf[1024]; long retvalue=Cmd1->Parameters->Item[(short)0]->Value; long p2=Cmd1->Parameters->Item[(short)1]->Value; long p3=Cmd1->Parameters->Item[(short)2]->Value; int recordcount=0; if(Rs1->State==ADODB::adStateClosed) MessageBox(NULL,"no recordset is returned from the stored procedure","Information",MB_OK); else recordcount = Rs1->GetRecordCount(); wsprintf(tcbuf,"retvalue of the stored procedure:%d,input value:%d,output value:%d,record count :%d",retvalue,p2,p3,recordcount); MessageBox(NULL,tcbuf,"Output",MB_OK); } catch(_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); printf("\nCOM error occurred, Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription); PrintProviderError(Conn1); } return 0; } VOID PrintProviderError(ADODB::_ConnectionPtr pConnection) { // Print Provider Errors from Connection object. // pErr is a record object in the Connection's Error collection. ADODB::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("\n\t Error number: %x\t%s", pErr->Number, (LPCSTR)pErr->Description); } } }
//ODBC open SQL server database CDatabase db; db.OpenEx("DRIVER={SQL Server};Server=Cell;DATABASE=zhg;UID=sa;PWD=;");//change Cell to your computer name //change zhg to your database name CRecordset rs(&db); rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select type,AVG(price)'average price' From title where type<>'business' group by type"); while(rs.IsEOF()==false) { CString str; CString strtype; rs.GetFieldValue((short)0,strtype); CDBVariant varprice; rs.GetFieldValue(1,varprice); str.Format("type:%s,avg(price):%f",strtype,varprice.m_dblVal); AfxMessageBox(str); rs.MoveNext(); } rs.Close(); db.Close();
如有正确答案
能否请发至我的信箱
[email protected]
非常感谢
//execute stored procedure , get returned value, output value, and returned recordset.
#include "stdafx.h"
#import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" rename( "EOF", "adoEOF" )
struct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
void PrintProviderError(ADODB::_ConnectionPtr pConnection);int main(int argc, char* argv[])
{
printf("Create a adParamReturnValue for Command object to get return value of stored procedure\n");
printf("Reference :MSDN---Append and CreateParameter Methods Example (VC++)\n");
ADODB::_ConnectionPtr Conn1;
ADODB::_CommandPtr Cmd1;
ADODB::_RecordsetPtr Rs1;
ADODB::_ParameterPtr retParam= NULL;
ADODB::_ParameterPtr inParam=NULL;
ADODB::_ParameterPtr outParam=NULL;
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2 (DISP_E_PARAMNOTFOUND, VT_ERROR);
_bstr_t bstrConnect( L"driver={sql server};server=Cell;Database=zhg;UID=sa;PWD=;" );
// the following stored procedure return 12345 int value
_bstr_t bstrCreate ( L"create proc sp_AdoTest( @OutParam int OUTPUT,@InParam int ) "
L"as "
L"select @OutParam = @InParam + 10 "
L"select * from Table1"
L"return 12345" );
_bstr_t bstrSP(L"sp_Adotest" );
try
{
_bstr_t bstrEmpty;
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
Cmd1.CreateInstance( __uuidof( ADODB::Command ) );
Rs1.CreateInstance(__uuidof(ADODB::Recordset));
// Establish connection.
Conn1->ConnectionString = bstrConnect;
Conn1->Open( bstrConnect, bstrEmpty, bstrEmpty, -1 );
// Open recordset.
Cmd1->ActiveConnection = Conn1;
Cmd1->CommandText = bstrSP;
Cmd1->CommandType = ADODB::adCmdStoredProc;
retParam=Cmd1->CreateParameter(_bstr_t("Return"),ADODB::adInteger,ADODB::adParamReturnValue,sizeof(int));
Cmd1->Parameters->Append(retParam);
outParam = Cmd1->CreateParameter(_bstr_t("OutParam"),ADODB::adInteger,ADODB::adParamOutput,sizeof(int));
Cmd1->Parameters->Append(outParam);
inParam = Cmd1->CreateParameter(_bstr_t("InParam"),ADODB::adInteger,ADODB::adParamInput,sizeof(int),_variant_t( (long) 10 ));
inParam->Value=_variant_t( (long) 10 );
Cmd1->Parameters->Append(inParam);
Cmd1->Parameters->Refresh();
Cmd1->Parameters->Item[ _variant_t( _bstr_t("@InParam") ) ]->Value =_variant_t( (long) 11 );
Rs1->put_CursorLocation(ADODB::adUseClient);
Rs1->Open((_variant_t((IDispatch *) Cmd1)),vtEmpty,ADODB::adOpenStatic,
ADODB::adLockReadOnly, -1);
// Get return value of the stored procedure.adCmdUnknown
TCHAR tcbuf[1024];
long retvalue=Cmd1->Parameters->Item[(short)0]->Value;
long p2=Cmd1->Parameters->Item[(short)1]->Value;
long p3=Cmd1->Parameters->Item[(short)2]->Value;
int recordcount=0;
if(Rs1->State==ADODB::adStateClosed)
MessageBox(NULL,"no recordset is returned from the stored procedure","Information",MB_OK);
else
recordcount = Rs1->GetRecordCount();
wsprintf(tcbuf,"retvalue of the stored procedure:%d,input value:%d,output value:%d,record count :%d",retvalue,p2,p3,recordcount);
MessageBox(NULL,tcbuf,"Output",MB_OK);
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\nCOM error occurred, Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
PrintProviderError(Conn1);
}
return 0;
}
VOID PrintProviderError(ADODB::_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ADODB::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("\n\t Error number: %x\t%s", pErr->Number, (LPCSTR)pErr->Description);
}
}
}
CDatabase db;
db.OpenEx("DRIVER={SQL Server};Server=Cell;DATABASE=zhg;UID=sa;PWD=;");//change Cell to your computer name
//change zhg to your database name
CRecordset rs(&db);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select type,AVG(price)'average price' From title where type<>'business' group by type");
while(rs.IsEOF()==false)
{
CString str;
CString strtype;
rs.GetFieldValue((short)0,strtype);
CDBVariant varprice;
rs.GetFieldValue(1,varprice);
str.Format("type:%s,avg(price):%f",strtype,varprice.m_dblVal);
AfxMessageBox(str);
rs.MoveNext();
}
rs.Close();
db.Close();
首先配置好ODBC
再在CLASSWAZARD新建一个类,派生于CRecordSet类.
选择相应的数据库及表,就行了.