How to call stored procedures using ADO?
In this example, I am using mcb.krz database which is an access database. The table i do have in example is 'Access' that has two fields, i.e., Name and HTML, both are Text type fields. Microsoft.Jet.OLEDB.3.51 is OLE_DB provider for MS Access. To use ADO, you must have OLE-DB provider for that database.
How to call stored procedures in ADO ?
_ConnectionPtr pCon;
_RecordsetPtr pRs;
_ConnandPtr pCommand;
// Data source name which is connected to database. You can used database name besides DSN.
bstr_t bstr = L"DSN=Kruse; ";
// Create instance of connection object
pCon.CreateInstance(__uuidof(Connection));
// Create instance of command object
pCommand.CreateInstance(__uuidof(Command));
//Open connection object
pCon->Open(bstr, (BSTR)NULL, (BSTR)NULL, -1);
pCommand->ActiveConnection = pCon ;
// tell the command object to use stored procedure
pCommnad->CommandType = adCmdStoredProc ;
// pass stored procedure name here
pCommnad->CommandText = _bstr_t("myStoredProc");
_variant_t vNull;
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
// execute stored procedure which returns a recordset object - pRs
pRs = pCommand->Execute( &vNull, &vNull, adCmdUnknown);
Now use this recordset to retrieve, update records or whatever.
How to call stored procedures in ADO that takes parameters?
_ConnectionPtr pCon;
_RecordsetPtr pRs;
_ConnandPtr pCommand;
// Data source name which is connected to database. You can used database name besides DSN. See previous examples.
bstr_t bstr = L"DSN=Kruse; ";
// Create instance of connection object
pCon.CreateInstance(__uuidof(Connection));
// Create instance of command object
pCommand.CreateInstance(__uuidof(Command));
//Open connection object
pCon->Open(bstr, (BSTR)NULL, (BSTR)NULL, -1);
pCommand->ActiveConnection = pCon ;
// tell the command object to use stored procedure
pCommnad->CommandType = adCmdStoredProc ;
// ADO Command object contains Parameters collection, which stores parameters that will passed to the stored procedure when Execute is called.
// pCommand->Parameters->Append appends a new parameter to the parameter collection for this command object. The argument passed to the
// Append function is the result of the pCommand->CreateParameter. Suppose I create a stored procedure "OrderSinceDate" which returns all orders since a particular
// date 12/01/1998.
// pass stored procedure name here
pCommnad->CommandText = _bstr_t("OrderSinceDate");
// Append parameter
pCommnad->Parameters->Append ( pCommand->CreateParameter ( _bstr_t("OrderDate"), adDBTimeStamp, adParamInput, 0, _variant_t( COleDateTime( 1999, 12, 1, 0, 0 , 0 )) ) );
// Where adDBTimeStamp is data type for this parameter, see DataTypeEnum for details, adParamInput input parameter, see ParameterDirectionEnum for details.
_variant_t vNull;
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
// execute stored procedure which returns a recordset object - pRs
pRs = pCommand->Execute( &vNull, &vNull, adCmdUnknown);
Now use this recordset to retrieve, update records or whatever.