Use ADO to call stored procedures

zhaozj2021-02-17  48

Use ADO to call stored procedures

Calling the stored procedure in the ADO has always been a problem that plagues everyone. In fact, the approach to the ADO calling process is said in many books, the standard approach is nothing more than the following steps: 1, generate and initialize a _commandptr object; 2, generate the parameters required to call the stored process, these parameters All _ParameterPtr object; 3, according to the order of _commandptr's Append method to provide parameters (including input parameters and output parameters); 4, specify the ADO connection you need to use for _Commandptr object; 5, use _commandptr The Execute method calls the stored procedure; 6, obtain the value of the return parameter from the results (if any).

The specific process here I don't detail here, I want to see the code comes with this article, it should be very clear.

Here I want some experience when I use ADO to call the stored procedure.

1. About CREATEPARAMETER functions

The prototype of this function is: CreateParameter (Name, Type, Direction, Size, Value)

Where Name is the name of the parameter, you can specify or not specify; type is a DATATYPEENUM value, specifying the category of the parameter, with an adINteger (integer), Adchar (character / string), etc .; Direction is a parameterDirectionenum value, Its value is adParaminput, AdParaminput, AdParamoutput, AdParamReturnValue, ADParamunkNown; SIZE is a value of a long type indicating that the parameter value is in one byte calculation, for example, for INT, this value can be taken as sizeof (int), pair LONG type, this value can be taken as a SizeOf (long), the length of the string can be used; Value is a value of a variant type, which is the value of this parameter.

It should be noted here that the TYPE parameter, the Direction parameter, and the size parameter must be consistent with the parameters when the stored procedure is defined.

For example, if there is a stored procedure below CREATE PROCEDURE SMS_Proc_Handle_All (@UserID Integer, @SourAddr Varchar (15), @DestAddr varchar (5000), @AvValue Single output, @ReturnInfo varchar (100) output) the values ​​of the parameters are sequentially Type For Adinteger, Adchar, Adchar, Adsingle, Adchar; Direction Parameters is AdParameterin, AdParameterin, AdParameterin, AdParameter, AdParameterin, AdParameter, AdParameter,, AdParameter, ADParameter,, ADPARAMETERIN, ADPARETEROUT; for input parameters, the value of size can be determined according to the actual value, and for output parameters, it is best to Definition determination (the size value of the ReturnInfo parameter in the upper example can be taken to 100).

2. About getting the parameters of Output

Get an OurPut parameter is the most concerned question, and it is also the most "difficult" problem, because according to the writing of books, you often get the output parameters, in fact, this problem is easy to solve: When calling _commandptr's Execute method, write CMMD -> EXECUTE (NULL, NULL, AdcmdStoredProc); not written into RecordSetptr Rec = cmmd-> execute (null, null, adcmdstoredproc); that is, do not return the value (I don't know why, but believe in me, things That's it). After this is executed, use cmmd-> parameters-> getItem ("xxxxxx") -> getValue (); ^^^^^^^ The name of the output parameter can get the value of the output parameter. The following is a partial code that calls the stored procedure via the ADO:

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

New Post(0)