开发者

asp.net cannot update database

开发者 https://www.devze.com 2022-12-25 02:14 出处:网络
string ConnectionString = WebConfigurationManager.ConnectionStrings[\"dbnameConnectionString\"].ConnectionString;
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

精彩评论

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

关注公众号