C # Call the stored procedure with output parameters

Source: Internet
Author: User

Create procedure progetpwd

@ Username varchar (20 ),

@ Password varchar (20) Output

As

Begin

Select @ Password = Password

From Users

Where username = @ username

End

 

 

String SQL = "Data Source = ..; initial catalog = test; Integrated Security = true ;";
Using (sqlconnection conn = new sqlconnection (SQL ))
{
Conn. open ();
Sqlcommand cmd = conn. createcommand ();
// Set the name of the stored procedure to be called
Cmd. commandtext = "getuserpwd ";
// Specify the name of the stored procedure passed to the database by the sqlcommand object instead of the SQL statement.
Cmd. commandtype = commandtype. storedprocedure;
Sqlparameter username = cmd. Parameters. Add (New sqlparameter ("@ username", sqldbtype. varchar, 20 ));
// Specify "@ username" as the input parameter
Username. Direction = parameterdirection. input;
// Assign a value to the @ username parameter
Username. value = txtusername. text;
Sqlparameter userpwd = cmd. Parameters. Add (New sqlparameter ("@ userpwd", sqldbtype. varchar, 20 ));
// Specify "@ userpwd" as the output parameter
Userpwd. Direction = parameterdirection. output;
// Execute
Cmd. executenonquery ();
// Obtain the value of the output parameter and assign the value to the name. Note that the value is of the object type, and the type rotation is required.
String passwrod = convert. tostring (CMD. Parameters ["@ userpwd"]. value );
MessageBox. Show (passwrod );

}

C # Call a stored procedure with output parameters

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.