Search This Blog

Wednesday, August 22, 2012

SqlCommand, SqlParameter c#

C# > Data SqlCommand SqlParameter 


SqlCommand
Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database.

SqlParameter 
Represents a parameter to a SqlCommand and optionally its mapping to DataSet columns

Example:

SqlCommand myCommand = new SqlCommand("dbo.[sp_copy]", connString);
myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter param = new SqlParameter("@ret_id", SqlDbType.Int);
param.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(param);

param = new SqlParameter("@Source", SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = asset_id;
myCommand.Parameters.Add(param);

myCommand.ExecuteNonQuery();

id = Convert.ToInt32(myCommand.Parameters[0].Value);