C# SQL查询异常
我在 .NET 2.0 中使用 C#,并尝试访问和操作数据库。我可以根据需要从数据库中读取任意多次,并且一切正常,但是一旦我尝试插入一个项目,我就会收到以下错误消息:
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
我已尝试查找此内容,但我能够找到修复程序要么不起作用,要么不适用。
我有以下代码:
using (SqlConnection conn = new SqlConnection(SQLConnectionString))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT [Col1] FROM [Table1] WHERE [Col2]='" + val2 + "'", conn);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count != 1)
{
SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
cmd.ExecuteNonQuery();
}
}
注意:我确信我的权限设置正确,因为 Visual Studio 可以使用相同的 SQLConnectionString 插入。另外,我对数据库还很陌生,所以如果我做任何明显错误的事情,请告诉我。
谢谢。
I'm using C# in .NET 2.0 and I'm trying to access and manipulate a database. I can read as many times from the DB as I want and everything works, but as soon as I try to insert an item I get the following error message:
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
I've tried to look this up, but the fixes I was able to find either didn't work or weren't applicable.
I have the following code:
using (SqlConnection conn = new SqlConnection(SQLConnectionString))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT [Col1] FROM [Table1] WHERE [Col2]='" + val2 + "'", conn);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count != 1)
{
SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
cmd.ExecuteNonQuery();
}
}
Note: I'm sure I have permissions set up properly, since Visual Studio can insert with the same SQLConnectionString. Also, I am still fairly new to databases, so if I'm doing anything blantently wrong, please let me know.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在调用 cmd.ExecuteNonQuery() 之前调用 conn.Open()。
Call conn.Open() before calling cmd.ExecuteNonQuery().
在执行命令之前,您必须打开连接
You have to open connection, before executing command
您需要在调用 con.ExecuteNonQuery 之前调用 con.open 并在其之后调用 con.Close 。 Dataadapter.fill 正在早期代码的幕后为您完成此操作。
You need to call con.open before you call con.ExecuteNonQuery and con.Close after it. Dataadapter.fill is doing it for you behind the scenes in the earlier code.
使用 cmd 之前检查 conn.IsOpen 属性。而且 SqlCommand 也是一次性对象,最好的做法是将其包含在“using”块中。
Check conn.IsOpen property before using cmd. And SqlCommand is disposable object too, its better practice to enclose it into "using" block.
上面没有人检查以确保连接已打开。
我在我的生产代码中使用它:
No-one above is checking to make sure the connection opened.
I use this in my production code:
如果连接未打开,
.Fill()
将打开连接,然后在连接完成后将其关闭(仅当它自己打开时)。这就是 Fill 起作用的原因。请参阅 MSDN。但
.ExecuteNonQuery()
不会执行此操作,因此您需要在ExecNonQuery 之前或 Fill 之前手动打开连接。
当您使用“using 块”时,您不需要显式 Close() 连接,但这不会是错误的。
The
.Fill()
opens the connection if it was not open and then closes it after it's done (only if it did open it itself). That's why that Fill did work. See MSDN.But the
.ExecuteNonQuery()
doesn't do that, so you need to manually open the connection, with aeither just before the ExecNonQuery or before the Fill.
As you are using a "using block", you don't need to explicitly Close() the connection, but that would not be wrong.
您似乎没有打开连接来执行更新(异常告诉您这一点)。
尝试一下
您可以在 MSDN 上获取更多详细信息和工作示例,网址为 http://msdn .microsoft.com/en-us/library/sd2728ad.aspx
You don't appear to be opening the connection to perform your update (the exception tells you this).
Try this
You can get more details and working example on MSDN at http://msdn.microsoft.com/en-us/library/sd2728ad.aspx
当您调用
dataAdapter.Fill(dataSet);
时,它将自动打开和关闭连接。因此,您需要在使用 insert 语句之前重新打开连接,或者将
DataAdapter
替换为SqlCommand
并保持连接打开,直到执行 insert 语句。When you call
dataAdapter.Fill(dataSet);
it will automatically open and close connection.So you need to reopen connection before using insert statement, or replace
DataAdapter
withSqlCommand
and keep connection opened until you execute insert statement.你没有打开连接。
本页向您展示如何使用 using 语句打开 SqlConnection。
你的朋友 C# 使用声明
You didn't open the connection.
This page shows you how to open a SqlConnection with the using statement.
Your Friend the C# Using Statement