Visual C # tutorial on Dynamically Operating SQL Server database instances (4 ):How to execute a stored procedure with Parameters
The previous article introduced the SQL statement execution method with parameters and the stored procedure execution method without parameters.How to execute a stored procedure with ParametersIt calls the SqlHelper class to execute the SqlHelper. ExecuteNonQuery () method. The example is;
SqlHelper. ExecuteNonQuery (conn, CommandType. StoredProcedure, sqlexec, myparm );
The four parameters are passed:
"Conn"-is a link character;
"CommandType. StoredProcedure"-indicates the type of SQL stored procedure to be executed;
"Sqlexec"-SQL stored procedure to be executed;
"Myparm"-is the passed parameter. It requires parameter initialization, parameter name assignment, set type and length, and obtain its value.
Protected void btnExecuteProcParm_Click (object sender, EventArgs e)
{
// Initialization parameters
SqlParameter myparm = new SqlParameter ();
// Obtain the parameter name
Myparm. ParameterName = "title ";
// Set the variable type and length
Myparm. SqlDbType = SqlDbType. VarChar; // is the data type VarChar in the system database used here?
Myparm. Size = 100; // depends on the definition in the data table
// Obtain the parameter value
Myparm. Value = "ExecuteNonQuery ";
// Obtain the stored procedure to be executed
String sqlexec = "sp_getGISnews ";
SqlCommand cmd = new SqlCommand ();
// Defines the storage range of object Resources. Once the using range ends, the resources occupied by the other party are released.
Using (SqlConnection conn = new SqlConnection (SqlHelper. ConnectionStringLocalTransaction ))
{
// Open the connection
Conn. Open ();
// Call the execution Method
SqlHelper. ExecuteNonQuery (conn, CommandType. StoredProcedure, sqlexec, myparm );
}
}
When ExecuteNonQuery () executes select, the result is always returned-1. ExecuteNonQuery () for Update, Insert, and Delete statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is-1. For more information, see the relevant content of your website.