Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net无法更新数据库_Asp.net - Fatal编程技术网

asp.net无法更新数据库

asp.net无法更新数据库,asp.net,Asp.net,有什么建议吗?稍微清理一下您想要尝试的代码您应该处理IDisposable对象: string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString; SqlConnection myConnection = new SqlConnection(ConnectionString); myConnection.Open(); try {

有什么建议吗?

稍微清理一下您想要尝试的代码您应该处理IDisposable对象:

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