VC calls the general method of the stored procedure (SQLServer)

xiaoxiao2021-04-03  185

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 fields-> getItem (m_param); if (m_param.vt! = vt_null) m_bstr = m_Param; Else M_BSTR = ""; Printf ("% s / n", (char *) m_bstr) } M_rs-> movenext ();} m_rs-> close (); m_conn-> close (); the above is the code of ordinary execution query SQL statement, simply, remove all protection and judgment code, for MS SQLServer If it is the stored procedure to perform the return result set, such as the ByroyalTy of Pubs, just replace "Select * from Titles" in the above code to "ByroyalTy 100", "Process Name Parameters 1, Parameter 2 ,. .."form.

转载请注明原文地址:https://www.9cbs.com/read-131546.html

New Post(0)