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