Use the Data Access Application block to get the return value of the stored procedure

Source: Internet
Author: User
Tags return
access|application| Stored Procedures

Today, a friend asked me how to get the return value of the stored procedure in the Data Access Application block, and I found that I did not mention this in my previous article, and now to add, the specific solution is as follows:

1, first set up a stored procedure with a return value, as an example, I will simply build a stored procedure, as follows:

CREATE proc Test
(
@id int
)
As
DECLARE @flag int
SELECT * from person where id= @id
If @ @rowcount > 0
Set @flag =1
Else
Set @flag =0
Return @flag

The method we want to get this return value in the program is as follows:

[TestMethod]
public void Testreturnvalue ()
{
Database db = Databasefactory.createdatabase ();
DbCommand Dbcomm = db. Getstoredproccommand ("test");
Db. Addinparameter (Dbcomm, "@id", dbtype.int32,1);
The key here is to add a parameter to the type returnvalue
Db. Addparameter (Dbcomm, "@RETURN_VALUE", dbtype.string, ParameterDirection.ReturnValue, "", DataRowVersion.Current, NULL);
Db. ExecuteNonQuery (Dbcomm);
int testvalue = (int) Dbcomm. parameters["@RETURN_VALUE"]. Value;
Assert.AreEqual (TestValue, 1);
}

With the above code we can get the return value of the stored procedure in the program. Previously written articles may have a lot of places did not say, I hope to have more friends to make comments, thank you!



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.