ASP. NET executes the stored procedure and obtains the returned values of the stored procedure.

Source: Internet
Author: User
A simple example is as follows:

1. Create a stored procedure for user authentication ):

Create procedure checkuser
(
@ Userid char (20 ),
@ Userpsw char (20)
)
As
If exists (select * From tb_userinfo where rtrim (userid) = @ userid and rtrim (userpsw) = @ userpsw)
Return 1
Else
Return 0

2. Asp.net executionCode:

// Connect to the database
String mystr = configurationmanager. etettings ["connectionstring"]. tostring ();
Sqlconnection myconn = new sqlconnection (mystr );
Myconn. open ();

// Create a sqlcommand object
Sqlcommand cmd = new sqlcommand ("Checkuser", Myconn );
Cmd. commandtype = commandtype. storedprocedure;

// Add Parameters
Sqlparameter userid = cmd. Parameters. Add ("@ userid", sqldbtype. Char );
Sqlparameter userpsw = cmd. Parameters. Add ("@ userpsw", sqldbtype. Char );
Sqlparameter returnvalue = cmd. Parameters. Add ("@ returnvalue", sqldbtype. INT );

// Specify whether the parameter can be input, output, or returnvalue)
Userid. Direction = parameterdirection. input;
Userpsw. Direction = parameterdirection. input;
Returnvalue. Direction = parameterdirection. returnvalue;

// Assign a value to the parameter passed to the stored procedure
Userid. value = tbusername. Text. Trim ();
Userpsw. value = tbpsw. Text. Trim ();

// Execute the SQL stored procedure
Cmd. executenonquery ();

// obtain and determine the return value
If (INT) returnvalue. value == 1)
{< br> session ["user"] = tbusername. text. trim ();
response. write ("current user:" + session ["user"]);
}< br> else
{< br> response. write ("the user does not exist! ");
}

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.