|
This is how we can call the stored procedure mentioned in the previous example: (C# and ADO.NET)
SqlConnection conn = new SqlConnection("connection string here");
SqlCommand command = new SqlCommand("testProcedure", conn);
//Defining command and parameters are here
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@inputVar1", SqlDbType.Int).Value = 1;
command.Parameters.Add("@outputVar1",SqlDbType.VarChar,50);
command.Parameters["@outputVar1"].Direction = ParameterDirection.Output;
//lets use sql parameter for a change for retrieving return values
//but this can also be done as shown above for input/output variables
SqlParameter paramReturnValue = new SqlParameter();
paramReturnValue.ParameterName = "@return_value";
paramReturnValue.SqlDbType = SqlDbType.Int;
paramReturnValue.Direction = ParameterDirection.ReturnValue;
//ready for execution
conn.Open();
command.ExecuteNonQuery();
//Executed, now retrieving values.
int intReturnValue = (int)command.Parameters["@return_value"].Value;
string strOutputVar = command.Parameters["@outputVar1"].Value.ToString();
|