What is discussed here is that the ADO calls stored procedures, according to MSDN examples, generally the following calling method (exception from MSDN):
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; _bstr_t strCnn ( "Provider = sqloledb; Data Source = srv;" "Initial Catalog = Pubs; User Id = sa; Password =;"); _bstr_t strMessage, strAuthorID; int intRoyalty; VARIANT vtRoyalty; try {/ / Open a Connection TESTHR (pConnection.CreateInstance (__ uuidof (Connection)));. hr = pConnection-> Open (strCnn, "", "", NULL); pConnection-> CursorLocation = adUseClient; // Open Command Object with one Parameter TESTHR (pcmdByRoyalty.CreateInstance (__ uuidof (Command))); pcmdByRoyalty-> CommandText = "byroyalty"; pcmdByRoyalty-> CommandType = adCmdStoredProc;. // 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); // processing result omitted ... pRstByRoyalty-> Close (); pRstAuthors-> Close () PConnection-> Close ();
} Catch (_com_error & e) {// Accidental Processing ...}} You must specify the commandType of the execution of the SQL statement is AdcmdStoredProc, and the stored procedure must create a CreateParameter to generate parameters so that the calling process has become a very troublesome thing. Different parameters must be generated for different stored procedures. One can be accepted, if the business logic of the project is more complicated, you need to rely on a lot of stored procedures, and the demand or function of the project may change or expand frequently, then it has become a nightmare. Is there a general way? Of course, you can implement only the universal query. _ConnectionPtr pConnection = NULL; m_Conn.CreateInstance (__ uuidof (Connection)); m_Conn-> ConnectionString = "Provider = sqloledb; Data Source = srv;" Initial Catalog = Pubs; User Id = sa; Password =; "; m_Conn-> Open ("" "," "," ", Null;
_variant_t m_param; _bstr_t m_bstr; int index = 0, nFieldCount; _RecordsetPtr m_Rs; m_Rs.CreateInstance (__ uuidof (Recordset)); m_Rs-> Open ( "select * from titles", (IDispatch *) (m_Conn-> m_Conn), adOpenStatic , AdlockReadOnly, AdcmdText); nfieldcount = m_rs-> fields-> count; while (! M_rs-> endoffile) {for (index = 0; index