Stored Procedure: OUT parameter
How to use OUT parameter in stored procedure? and how to use that OUT parameter in code behind?..................
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Subhendu DePosted Dec 13, 2010, 8:34 AM
CREATE PROCEDURE prcTest
@pIn nvarchar(50),
@pOut nvarchar(50) OUT
AS
BEGIN
Select @pOut = 'Hello' + @pIn
END
GO
This is code-behind
string _connectString = @"Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True";
using(SqlConnection _connection = new SqlConnection(_connectString))
{
SqlCommand _command = new SqlCommand();
_command.Connection = _connection;
_command.CommandText = "prcTest";
_command.CommandType = CommandType.StoredProcedure;
SqlParameter[] _parameters = new SqlParameter[2];
_parameters[0] = new SqlParameter();
_parameters[0].ParameterName = "@pIn";
_parameters[0].SqlDbType = SqlDbType.NVarChar;
_parameters[0].Size = 50;
_parameters[0].Direction = ParameterDirection.Input;
_parameters[0].Value = "World";
_parameters[1] = new SqlParameter();
_parameters[1].ParameterName = "@pOut";
_parameters[1].SqlDbType = SqlDbType.NVarChar;
_parameters[1].Size = 50;
_parameters[1].Direction = ParameterDirection.Output;
_command.Parameters.AddRange(_parameters);
_connection.Open();
_command.ExecuteNonQuery();
_connection.Close();
Console.WriteLine(_command.Parameters["@pOut"].Value);
}
Thanks.....