Use ADO to execute the Stored Procedure

Source: Internet
Author: User
I did not write this article, but I collected it.
Calling stored procedures in ado has always been a problem for everyone.
In fact
Methods have been mentioned in many books. The standard practice is simply based on the following steps:
1. Generate and initialize a _ commandptr object;
2. Generate parameters required to call the stored procedure. These parameters are _ parameterptr objects;
3. Use the _ commandptr append method to provide parameters (including input and output parameters) for the stored procedure in sequence );
4. specify the required ADO connection for the _ commandptr object;
5. Use the execute method of _ commandptr to call the stored procedure;
6. Obtain the value of the returned parameter from the result (if any ).


For the createparameter function:
Createparameter (name, type, direction, size, value)

Here, name is the parameter name, which can be specified or not specified;

Type is a datatypeenum value. It specifies the type of the parameter. The values include adinteger and adchar;

Direction is a parameterdirectionenum value. Its values include adparaminput, adparaminputoutput, adparamoutput, adparamreturnvalue, and adparamunknown;

Size is a long value, indicating the maximum length of the parameter value in bytes. For example, for int type, this value can be sizeof (INT), for long type, this value can be sizeof (long). For string type, the length of this string can be used. value is a variant type value, which is the value of this parameter.

Note that the type, direction, and size parameters must be consistent with the parameters defined in the stored procedure,

If you have the following stored procedure
Create procedure sms_proc_handle_all
(
@ Userid integer,
@ Souraddr varchar (15 ),
@ Destaddr varchar (5000 ),
@ Avvalue single output,
@ Returninfo varcharar (100) Output
)

The values of the type parameter are adinteger, adchar, adchar, adsingle, and adchar;
The value of direction is adparameterin, adparameterout, and adparameterout;
For input parameters, the size value can be determined based on the actual value,
For output parameters, it is best to determine according to the definition (the size value of the returninfo parameter in the previous example can be set to 100 ).

The following code calls a stored procedure through ADO:
_ Commandptr cmmd;
Hresult hR = cmmd. createinstance (_ uuidof (command ));
If (failed (HR ))
{
Afxmessagebox ("An error occurred while creating the _ commandptr object in newnetdatabase ");
Return 0;
}
_ Parameterptr Param;
Param = cmmd-> createparameter (""/* nettype */, adtinyint, adparaminput, sizeof (byte), (byte) (m_nnettype + 1 ));
Cmmd-> parameters-> append (PARAM );
Param = cmmd-> createparameter (""/* name */, advarchar, adparaminput, m_strname.getlength () + 1, _ variant_t (m_strname ));
Cmmd-> parameters-> append (PARAM );
Param = cmmd-> createparameter (""/* desp */, advarchar, adparaminput, m_strdesp.getlength () + 1, _ variant_t (m_strdesp )); cmmd-> parameters-> append (PARAM );
// Return parameters. The ID of the new network is returned.
Param = cmmd-> createparameter ("newnetid"/* netid */, adinteger, adparamoutput, sizeof (long), (long) m_nnewnetid );
Cmmd-> parameters-> append (PARAM );
Cmmd-> commandtext = _ bstr_t ("gsdt_newnet"); // name of the stored procedure
Cmmd-> activeconnection = m_pconptr; // The required ADO connection
Cmmd-> commandtype = adw.storedproc;
Cmmd-> execute (null, null, ad1_storedproc );
M_nnewnetid = (long) cmd-> parameters-> getitem ("newnetid")-> getvalue (); // return value through the Parameter
Cmmd. Detach ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.