string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
try
{
string qry = "UPDATE customers SET firstname=@firstname WHERE cid=1";
SqlCommand insertQuery = new SqlCommand(qry, myConnection);
insertQuery.Parameters.Add(new SqlParameter("@firstname"开发者_运维问答, txtFirstname.Text));
insertQuery.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception ee)
{
}
Any suggestions?
A little cleanup of your code you may like to try (you should dispose IDisposable objects):
string ConnectionString = // ...
using (var connection = new SqlConnection(ConnectionString))
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = "UPDATE customers SET firstname = @firstname WHERE cid=1";
command.Parameters.AddWithValue("@firstname", txtFirstname.Text);
var rowsUpdated = command.ExecuteNonQuery();
}
精彩评论