The previous article introduced the execution methods of SQL statements without parameters and stored procedures without parameters. This article describes the execution methods of SQL statements with parameters, it calls the SqlHelper class to execute SqlHelper. executeNonQuery () method. The example is;
Int val = SqlHelper. ExecuteNonQuery (conn, CommandType. Text, SQL, myparm );
The four parameters are passed:
"Conn"-is a link character;
"CommandType. Text"-type of the SQL command to be executed;
"SQL"-the SQL command 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 btnExecuteSQLParm_Click (object sender, EventArgs e)
{
// Initialization parameters
SqlParameter myparm = new SqlParameter ();
// Obtain the parameter name
Myparm. ParameterName = "title ";
// The type and length of the unspecified variable
Myparm. SqlDbType = SqlDbType. VarChar;
Myparm. Size = 100;
// Obtain the parameter value
Myparm. Value = "ExecuteNonQuery ";
// Obtain the SQL command to be executed
String SQL = "select title from menularticle where title = @ title ";
SqlCommand cmd = new SqlCommand ();
// Define the storage range of object Resources. When the value of using is reached, the resources occupied by the other party are released.
Using (SqlConnection conn = new SqlConnection (SqlHelper. ConnectionStringLocalTransaction) // note that because conn is already used in SqlHelper. cs, it cannot be renamed here, so use conn1 instead.
{
// Open the link
Conn. Open ();
// Call the execution Method
Int val = SqlHelper. ExecuteNonQuery (conn, CommandType. Text, SQL, myparm );
Response. Write ("");
}
}
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.