asp.net无法更新数据库
有什么建议吗?稍微清理一下您想要尝试的代码您应该处理IDisposable对象:asp.net无法更新数据库,asp.net,Asp.net,有什么建议吗?稍微清理一下您想要尝试的代码您应该处理IDisposable对象: string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString; SqlConnection myConnection = new SqlConnection(ConnectionString); myConnection.Open(); try {
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)
{
}
您是否遇到异常?因此,您的数据库中没有cid=1?如果他遇到异常,则它已被捕获而未被执行@我只是为了测试而放弃try/catch。另外,如果您在关闭之前遇到异常,您将与发布的代码保持一个打开的连接。为了让我们能够帮助您,您必须更具体一点。执行此语句后rowsUpdated的值是多少?
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();
}