This article describes how to use C # To call a stored procedure with a returned value. The input parameter input, output parameter output, and returned value returnvalue are used. In fact, when a stored procedure is called in. net, there is another parameter type: InputOutput, which can be both input and output. Today I wrote a program segment and tried it. I learned how to use it.
(1) create a stored procedure
Use [Addresslist]
Go
/***** Object: storedprocedure [DBO]. [test] script Date: 12/19/2011 20:23:15 ******/
Set ansi_nulls on
Go
Set quoted_identifier on
Go
Create procedure [DBO]. [test]
@ Groupname nvarchar (50) Output
As
Begin
Select @ groupname = groupname from contactgroup where groupname like '%' + @ groupname + '%'
End
(2) execute in the query Analyzer
Use [Addresslist]
Go
Declare @ return_value int, @ groupname nvarchar (50)
Set @ groupname = 'old'
Exec [DBO]. [test]
@ Groupname output
Select @ groupname
(3) compile C # code call, specify the input value, and obtain the output value
Public String sqlparameterinputoutput ()
{
Using (sqlconnection conn = new sqlconnection (@ "Server =. \ sqlexpress; database = Addresslist; Integrated Security = true "))
{
Sqlcommand cmd = new sqlcommand ("test", Conn );
Cmd. commandtype = commandtype. storedprocedure;
Sqlparameter P = new sqlparameter ("@ groupname", sqldbtype. nvarchar, 50 );
P. Direction = parameterdirection. InputOutput;
P. value = "old ";
Cmd. Parameters. Add (P );
Conn. open ();
Cmd. executenonquery ();
String pvalue = P. value. tostring ();
Return pvalue;
}
}