//Wait here for the user to see the output. printf("\n\nPress any key to continue..."); getch(); ::CoUninitialize(); } /////////////////////////////////////////////////////////// // // // AppendX Function // // // /////////////////////////////////////////////////////////// VOID AppendX(VOID) { HRESULT hr = S_OK; // Define ADO object pointers. // Initialize pointers on define. // These are in the ADODB:: namespace. _RecordsetPtr pRstByRoyalty = NULL; _RecordsetPtr pRstAuthors = NULL; _CommandPtr pcmdByRoyalty = NULL; _ParameterPtr pprmByRoyalty = NULL; _ConnectionPtr pConnection = NULL; //Define Other variables IADORecordBinding *picRs = NULL; //Interface Pointer declared.(VC++ Extensions) CEmployeeRs emprs; //C++ class object _bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;" "Initial Catalog=pubs;Integrated Security=SSPI;"); _bstr_t strMessage, strAuthorID; int intRoyalty; VARIANT vtRoyalty; try { //Open a Connection. TESTHR(pConnection.CreateInstance(__uuidof(Connection))); hr = pConnection->Open(strCnn,"","",adConnectUnspecified); pConnection->CursorLocation = adUseClient; //Open Command Object with one Parameter TESTHR(pcmdByRoyalty.CreateInstance(__uuidof(Command))); pcmdByRoyalty->CommandText = "byroyalty"; pcmdByRoyalty->CommandType = adCmdStoredProc; //Get parameter value and append parameter printf("Enter Royalty: "); scanf("%d",&intRoyalty); //Define Integer/variant. vtRoyalty.vt = VT_I2; vtRoyalty.iVal = intRoyalty; pprmByRoyalty = pcmdByRoyalty->CreateParameter("percentage",adInteger,adParamInput,sizeof(int),vtRoyalty); pcmdByRoyalty->Parameters->Append(pprmByRoyalty);
pprmByRoyalty->Value = vtRoyalty;
//Create Recordset by executing the command pcmdByRoyalty->ActiveConnection = pConnection; pRstByRoyalty = pcmdByRoyalty->Execute(NULL,NULL,adCmdStoredProc); //Open the authors table to get author names for display TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset))); //You have to explicitly pass the default Cursor type and LockType to the Recordset here hr = pRstAuthors->Open("authors",_variant_t((IDispatch*)pConnection,true),adOpenForwardOnly,adLockReadOnly,adCmdTable);
//Open an IADORecordBinding interface pointer which we'll use for Binding Recordset to a class TESTHR(pRstAuthors->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs)); //Bind the Recordset to a C++ Class here TESTHR(picRs->BindToRecordset(&emprs)); //Print current data in the recordset, adding //author names from author table. printf("Authors with %d percent royalty ",intRoyalty); while(!(pRstByRoyalty->EndOfFile)) { strAuthorID = pRstByRoyalty->Fields->Item["au_id"]->Value; pRstAuthors->Filter = "au_id = '"+strAuthorID+"'"; printf("\n" "%s, %s %s",emprs.lau_idStatus == adFldOK ? emprs.m_szau_id : "<NULL>",\ emprs.lau_fnameStatus == adFldOK ? emprs.m_szau_fname : "<NULL>",\ emprs.lau_lnameStatus == adFldOK ? emprs.m_szau_lname : "<NULL>"); pRstByRoyalty->MoveNext();
} //Release the IADORecordset Interface here if (picRs) picRs->Release();
pRstByRoyalty->Close(); pRstAuthors->Close(); pConnection->Close(); } catch(_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); PrintProviderError(pConnection); printf("\n Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription); }} /////////////////////////////////////////////////////////// // // // 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\n Error Description: %s\n", pErr->Number,(LPCSTR) pErr->Description); } } } // EndAppendCpp
ODBC: class chcode : public CRecordset chcode::chcode(CDatabase* pdb) : CRecordset(pdb) { //{{AFX_FIELD_INIT(chcode) m_oldpassword=""; m_newpassword=""; m_username=""; //}}AFX_FIELD_INIT m_nDefaultType = snapshot; m_nParams=4; } CString chcode::GetDefaultConnect() { return _T("ODBC;DSN="); }CString chcode::GetDefaultSQL() { return _T(""); }void chcode::DoFieldExchange(CFieldExchange* pFX) { //{{AFX_FIELD_MAP(chcode) pFX->SetFieldType(CFieldExchange ::outputParam); //set the field type to outputParam for the return value RFX_Long(pFX, _T("return_value"), m_retreturn_value); //bind the return value to the variable pFX->SetFieldType(CFieldExchange ::inputParam); //reset the field type to inputParam RFX_Text(pFX, "@old", m_oldpassword);//,255,SQL_CHAR,0); RFX_Text(pFX, "@new", m_newpassword);//,255,SQL_CHAR,0); //call the new rfx_Text to get the character output params RFX_Text(pFX, "@loginame", m_username);//,255,SQL_CHAR,0); //}}AFX_FIELD_MAP }//调用存储过程修改用户密码 CDatabase db1; s1.Format("ODBC;UID=sa;PWD=%s","11111"); db1.Open("report1",false,false,s1); chcode chrs(&db1); //从CRecordSet 继承的新类 chrs.m_newpassword=in.m1; chrs.m_oldpassword=s3; chrs.m_username="report"; chrs.Open( AFX_DB_USE_DEFAULT_TYPE ,_T("{?=CALL sp_password(?,?,?)}")); chrs.Close(); db1.Close();
自己看
// BeginAppendCpp
#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 "AppendX.h"//Function declaration
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void AppendX(VOID);
void PrintProviderError(_ConnectionPtr pConnection);///////////////////////////////////////////////////////////
// //
// Main Function //
// //
///////////////////////////////////////////////////////////
void main()
{
HRESULT hr = S_OK; if(FAILED(::CoInitialize(NULL)))
return; AppendX();
//Wait here for the user to see the output.
printf("\n\nPress any key to continue...");
getch();
::CoUninitialize();
}
///////////////////////////////////////////////////////////
// //
// AppendX Function //
// //
///////////////////////////////////////////////////////////
VOID AppendX(VOID)
{ HRESULT hr = S_OK; // Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstByRoyalty = NULL;
_RecordsetPtr pRstAuthors = NULL;
_CommandPtr pcmdByRoyalty = NULL;
_ParameterPtr pprmByRoyalty = NULL;
_ConnectionPtr pConnection = NULL;
//Define Other variables
IADORecordBinding *picRs = NULL; //Interface Pointer declared.(VC++ Extensions)
CEmployeeRs emprs; //C++ class object _bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
"Initial Catalog=pubs;Integrated Security=SSPI;"); _bstr_t strMessage, strAuthorID; int intRoyalty;
VARIANT vtRoyalty; try
{
//Open a Connection.
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
hr = pConnection->Open(strCnn,"","",adConnectUnspecified);
pConnection->CursorLocation = adUseClient; //Open Command Object with one Parameter
TESTHR(pcmdByRoyalty.CreateInstance(__uuidof(Command)));
pcmdByRoyalty->CommandText = "byroyalty";
pcmdByRoyalty->CommandType = adCmdStoredProc; //Get parameter value and append parameter
printf("Enter Royalty: ");
scanf("%d",&intRoyalty); //Define Integer/variant.
vtRoyalty.vt = VT_I2;
vtRoyalty.iVal = intRoyalty;
pprmByRoyalty = pcmdByRoyalty->CreateParameter("percentage",adInteger,adParamInput,sizeof(int),vtRoyalty);
pcmdByRoyalty->Parameters->Append(pprmByRoyalty);
pprmByRoyalty->Value = vtRoyalty;
//Create Recordset by executing the command
pcmdByRoyalty->ActiveConnection = pConnection;
pRstByRoyalty = pcmdByRoyalty->Execute(NULL,NULL,adCmdStoredProc); //Open the authors table to get author names for display
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset))); //You have to explicitly pass the default Cursor type and LockType to the Recordset here
hr = pRstAuthors->Open("authors",_variant_t((IDispatch*)pConnection,true),adOpenForwardOnly,adLockReadOnly,adCmdTable);
//Open an IADORecordBinding interface pointer which we'll use for Binding Recordset to a class
TESTHR(pRstAuthors->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs)); //Bind the Recordset to a C++ Class here
TESTHR(picRs->BindToRecordset(&emprs)); //Print current data in the recordset, adding
//author names from author table.
printf("Authors with %d percent royalty ",intRoyalty); while(!(pRstByRoyalty->EndOfFile))
{
strAuthorID = pRstByRoyalty->Fields->Item["au_id"]->Value;
pRstAuthors->Filter = "au_id = '"+strAuthorID+"'"; printf("\n" "%s, %s %s",emprs.lau_idStatus == adFldOK ? emprs.m_szau_id : "<NULL>",\
emprs.lau_fnameStatus == adFldOK ? emprs.m_szau_fname : "<NULL>",\
emprs.lau_lnameStatus == adFldOK ? emprs.m_szau_lname : "<NULL>"); pRstByRoyalty->MoveNext();
} //Release the IADORecordset Interface here
if (picRs)
picRs->Release();
pRstByRoyalty->Close();
pRstAuthors->Close();
pConnection->Close();
} catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description()); PrintProviderError(pConnection); printf("\n Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}}
///////////////////////////////////////////////////////////
// //
// 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\n Error Description: %s\n", pErr->Number,(LPCSTR) pErr->Description);
}
}
}
// EndAppendCpp
class chcode : public CRecordset
chcode::chcode(CDatabase* pdb)
: CRecordset(pdb)
{
//{{AFX_FIELD_INIT(chcode)
m_oldpassword="";
m_newpassword="";
m_username="";
//}}AFX_FIELD_INIT
m_nDefaultType = snapshot;
m_nParams=4;
}
CString chcode::GetDefaultConnect()
{
return _T("ODBC;DSN=");
}CString chcode::GetDefaultSQL()
{
return _T("");
}void chcode::DoFieldExchange(CFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(chcode)
pFX->SetFieldType(CFieldExchange ::outputParam); //set the field type to outputParam for the return value
RFX_Long(pFX, _T("return_value"), m_retreturn_value); //bind the return value to the variable
pFX->SetFieldType(CFieldExchange ::inputParam); //reset the field type to inputParam
RFX_Text(pFX, "@old", m_oldpassword);//,255,SQL_CHAR,0);
RFX_Text(pFX, "@new", m_newpassword);//,255,SQL_CHAR,0); //call the new rfx_Text to get the character output params
RFX_Text(pFX, "@loginame", m_username);//,255,SQL_CHAR,0);
//}}AFX_FIELD_MAP
}//调用存储过程修改用户密码 CDatabase db1;
s1.Format("ODBC;UID=sa;PWD=%s","11111");
db1.Open("report1",false,false,s1);
chcode chrs(&db1); //从CRecordSet 继承的新类
chrs.m_newpassword=in.m1;
chrs.m_oldpassword=s3;
chrs.m_username="report";
chrs.Open( AFX_DB_USE_DEFAULT_TYPE ,_T("{?=CALL sp_password(?,?,?)}"));
chrs.Close();
db1.Close();
retcode = SQLPrepare(hstmt1,(unsigned char *)SQLstr,SQL_NTS);
retcode = SQLExecute(hstmt1);//test1是过程名