Implementation Code for ASP. NET to obtain the returned values of Stored Procedures

Source: Internet
Author: User

1. First, create a table (UserInfo ).
Main field Id, uname, upass.
2. Create a stored procedure with a returned value Copy codeThe Code is as follows: create proc proc_addUserInfo
@ Uname varchar (50 ),
@ Upass varchar (50 ),
@ Returnc int output
As
Begin
Insert into erp_user (u_name, u_pass) values (@ uname, @ upass)
Set @ Returnc = isnull (@ identity, 0)
End

Here, the returned value is the Id of the added record.
Background code:Copy codeThe Code is as follows: SqlParameter [] para = new SqlParameter []
{
New SqlParameter ("@ uname", "2012 "),
New SqlParameter ("@ upass", "2012 ")
};
String SQL _para = "dbo. proc_addUserInfo ";
CommandType type = CommandType. StoredProcedure;
SQLHelper sqlh = new SQLHelper ();
Int Id = sqlh. ExecuteNoQuery (SQL _para, type, para );

SQLHelper:Copy codeThe Code is as follows: public int ExecuteNoQuery (string SQL, CommandType type, params SqlParameter [] values)
{
Using (SqlCommand cmd = new SqlCommand (SQL, Conn ))
{
Cmd. CommandType = type;
If (values! = Null)
{
Cmd. Parameters. AddRange (values );
}
SqlParameter Retvar = cmd. Parameters. Add ("@ Returnc", SqlDbType. Int );
Retvar. Direction = ParameterDirection. Output;
Int count = cmd. ExecuteNonQuery ();
Return (int) Retvar. Value;
}
}

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.