简体   繁体   English

尝试使用asp.net更新数据库时出现异常错误

[英]Exception Error in trying to update database using asp.net

How come I'm getting this error while trying to update my database? 为什么我在尝试更新数据库时遇到此错误?

ExecuteNonQuery requires an open and available Connection. ExecuteNonQuery需要一个开放且可用的连接。 The connection's current state is closed 连接的当前状态已关闭

Here is the code: 这是代码:

cmd1 = new SqlCommand("UPDATE [guitarBrands] SET type = @type, name = @name, image = @image WHERE id = @id", con1);

con1.Open();

cmd1.Parameters.Add(new SqlParameter("type", newType.Text));
cmd1.Parameters.Add(new SqlParameter("name", newName.Text));
cmd1.Parameters.Add(new SqlParameter("image", newImage.Text));
cmd1.Parameters.Add(new SqlParameter("id", id));

cmd1.ExecuteNonQuery();

con1.Close();
cmd1.Parameters.Clear();

cmd = new SqlCommand("UPDATE [guitarItems] SET brand = @brand WHERE id = @id", con1);
con.Open();

cmd.Parameters.Add(new SqlParameter("brand", newName.Text));
cmd.Parameters.Add(new SqlParameter("id", id));

cmd.ExecuteNonQuery();
con.Close();

cmd.Parameters.Clear();

To avoid these sort of issues, it is recommended you utilize the tower of power . 为避免出现这类问题,建议您使用电源塔

using(var connection = new SqlConnection(dbConnection))
{
     connection.Open();
     using(var command = new SqlCommand(query, connection)
     {

     }

     using(var command = new SqlCommand(query, connection)
     {

     }
}

So the beauty of the tower of power, the using block will implement via within the given code block. 所以权力塔的美丽,使用块将在给定的代码块内实现。 So this will make it clear, that both these commands are utilizing the same connection from the using. 因此,这将清楚地表明,这两个命令都使用了与使用相同的连接。 Also, once the code is out of scope it will implement the IDispose , which will call the garbage collector to free up your resources. 此外,一旦代码超出范围,它将实现IDispose ,它将调用垃圾收集器来释放资源。

Also, should you choose. 另外,你应该选择。 The SqlCommand , accepts a parameter array. SqlCommand接受参数数组。 So if you utilize a method call, you could simply do: 因此,如果您使用方法调用,您可以简单地执行:

public static GetExample(string query, params SqlParameter[] parameters)
{
     using(var connection = new SqlConnection("YourDbConnection"))
     using(var command = new SqlCommand("YourQuery", connection))
     {
          connection.Open();
          if(parameters != null)
               if(parameters.Any())
                    command.Parameters.Add(parameters);

          command.ExecuteNonQuery();
     }
}

I can't recall if the collection is a add, add range, or concat. 我不记得该集合是添加,添加范围还是连续。 But either way the option exist. 但无论哪种方式存在选择。

cmd = new SqlCommand("UPDATE [guitarItems] SET brand = @brand WHERE id = @id", con1);

将此行替换为此

cmd = new SqlCommand("UPDATE [guitarItems] SET brand = @brand WHERE id = @id", con);

In your code you use Connexion "con1" in both Commands "cmd1" and "cmd". 在您的代码中,您在命令“cmd1”和“cmd”中使用Connexion“con1”。 It is OK to use just one connexion for both commands but then you should leave the connexion open until both command are executed. 对两个命令只使用一个连接是可以的,但是你应该将连接打开,直到两个命令都被执行。 In your case you choose to use a new connexion "con" for the second command but you reopen "con1". 在您的情况下,您选择为第二个命令使用新的连接“con”,但是您重新打开“con1”。 So you get an error because "con" is never opened. 所以你得到一个错误,因为永远不会打开“con”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Asp.Net更新mysql数据库 - Update the mysql database using Asp.Net 使用 ASP.NET MVC 更新数据库 - Update database using ASP.NET MVC 数据库连接; ASP.net中的null引用异常错误 - Database connection ;Null Reference Exception error in ASP.net 尝试使用ASP.NET将数据写入数据库 - 错误“必须声明标量变量”并且不确定原因 - Trying to write data to a database using ASP.NET - error “Must declare the scalar variable” and unsure why 尝试使用asp.net Web应用程序将数据插入SQL Server数据库,出现错误 - Trying to insert data into SQL Server database using asp.net web application, getting an error 试图使用asp.net和c#将数据插入数据库 - trying to insert data into a database using asp.net and c# 尝试使用asp.net创建sqlparameter集合时出现异常错误 - exception error in attempt to create an sqlparameter collection using asp.net ASP.NET Web应用程序尝试使用root以外的用户访问MySQL数据库时抛出异常 - ASP.NET web app throwing exception when trying to access MySQL database with user other than root 尝试将数据保存到数据库时使用C#和ASP.NET进行异常处理 - Exception handling with C# & ASP.NET when trying to save data to database ASP.NET更新网页上的数据库更新 - asp.net updateing webpage on database update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM