Some ideas about the ADO invocation of stored procedures (output parameters, return values)
Source: Internet
Author: User
Ado| Stored Procedure | experience
In a project, I need to use a stored procedure to access the data, in order to provide a more consistent interface to invoke, I do not use CreateParameter (), but rather call the Commandptr refresh () function to first query parameters from the database.
_connectionptr M_pconn;
M_pconn.createinstance (__uuidof (Connection));
M_pconn->open ("Driver={sql server};server=127.0.0.1;database=pub; Uid=sa; Pwd= "," "," ", 0);
M_pcommand->activeconnection = M_pconn;
M_pcommand->commandtext = "sp_xx";//Stored Procedure name
M_pcommand->putcommandtype (adCmdStoredProc);
M_pcommand->parameters->refresh ()//From database query parameter information
You can then assign values to each parameter:
Long cnt = M_pcommand->parameters->getcount ()//number of arguments obtained
for (long k=1;k<cnt;k++)
{//Because the return value is considered to be the first parameter in ADO, the first parameter is filtered out here with K=1
M_pcommand->parameters->getitem (k)->value = xxx;//Assign values to parameters in the order of stored procedures
}
You can now execute this stored procedure.
M_precordset = M_pcommand->execute (0,0,adcmdstoredproc);
This time, if the next use
_variant_t ret_val = M_pcommand->parameters->getitem ((long) 0)->value;
Then you won't get a value.
And if you call like this, you get the return value.
M_precordset->close ();
_variant_t Output_para = M_pcommand->parameters->getitem ((long) 0)->value;
MS Ado.net's response to this phenomenon is:
You can have a stored procedure as a function in your code. The function doesn ' t return a value until it has executed all of its code. If the stored procedure returns results and you haven ' t finished processing of these results, the stored procedure ' t rea Lly finished executing. Until ' ve closed the DataReader, the return and output parameters of your Command won ' t contain the values returned by Your stored procedure.
This means that the execute () function should be considered to be returned correctly until the M_precordset is turned off.
The processing of output parameters is the same as this, because the return value itself is treated as an output parameter.
In this way, we can get a stored procedure return value and result set, and for all stored procedures can be used the same, do not have to write code for a particular stored procedure, there is a certain commonality.
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