1) The code for executing a stored procedure without parameters is as follows:
SqlConnection conn=new SqlConnection ("connectionString");
SqlDataAdapter da = new SqlDataAdapter ();
Da.selectcommand = new SqlCommand ();
Da.selectCommand.Connection = conn;
Da.selectCommand.CommandText = "Nameofprocedure";
Da.selectCommand.CommandType = CommandType.StoredProcedure;
(2) The code to execute a stored procedure with parameters is as follows
SqlConnection conn=new SqlConnection ("connectionString");
SqlDataAdapter da = new SqlDataAdapter ();
Da.selectcommand = new SqlCommand ();
Da.selectCommand.Connection = conn;
Da.selectCommand.CommandText = "Nameofprocedure";
Da.selectCommand.CommandType = CommandType.StoredProcedure;
param = new SqlParameter ("@ParameterName", sqldbtype.datetime);
Param. Direction = ParameterDirection.Input;
Param. Value = Convert.todatetime (inputdate);
DA.SELECTCOMMAND.PARAMETERS.ADD (param);
If you need to add an output parameter:
param = new SqlParameter ("@ParameterName", sqldbtype.datetime);
Param. Direction = ParameterDirection.Output;
Param. Value = Convert.todatetime (inputdate);
DA.SELECTCOMMAND.PARAMETERS.ADD (param);
To get the return value of the stored procedure:
param = new SqlParameter ("@ParameterName", sqldbtype.datetime);
Param. Direction = ParameterDirection.ReturnValue;
How do I invoke a stored procedure with parameters in C #?