开发者

asp.net cannot update database

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜