vb.net System.Data.dll 中发生类型为“System.InvalidOperationException”的未处理异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17475839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
提问by user2550171
i am creating a connection to my database in 'sql server 2008 management edition ' from visual studio 2008.
我正在从 Visual Studio 2008 的“sql server 2008 management edition”中创建到我的数据库的连接。
i stored a procedure called "CTable" in my database and i want to execute it.(from visual studio)
我在我的数据库中存储了一个名为“CTable”的过程,我想执行它。(来自 Visual Studio)
this is my code:
这是我的代码:
Dim strConn As String
strConn = "Server=(local);Database=dbAjout;Integrated Security=True"
Dim MyConn As New SqlConnection(strConn)
MyConn.Open()
Dim cmd As SqlCommand
Dim query As String = "EXEC CTable"
cmd = New SqlCommand(query, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub
but i have this error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
但我有这个错误: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: The connection was not closed. The connection's current state is open."
附加信息:连接未关闭。连接的当前状态是打开的。”
what do you suggest??? im blocked, its been 4 hours !!!!
你有什么建议???我被屏蔽了,已经 4 小时了!!!!
回答by Styxxy
You open your connection twice. As per documentation (SqlConnection.Open()
):
您打开连接两次。根据文档 ( SqlConnection.Open()
):
Exceptions
InvalidOperationException
- Cannot open a connection without specifying a data source or server.
- or
- The connection is already open.
异常
InvalidOperationException
- 无法在不指定数据源或服务器的情况下打开连接。
- 或
- 连接已打开。
That is (probably) what causes the InvalidOperationException
to be thrown.
这(可能)是导致InvalidOperationException
抛出的原因。
回答by Karl Anderson
Change your code to this:
将您的代码更改为:
Dim strConn As String
strConn = "Server=(local);Database=dbAjout;Integrated Security=True"
Dim MyConn As New SqlConnection(strConn)
Dim cmd As SqlCommand
Dim query As String = "EXEC CTable"
cmd = New SqlCommand(query, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
You should consider the invoking the Dispose
pattern for your SqlConnection
by utilizing the Using
statement, like this:
您应该考虑通过使用语句Dispose
为您调用模式,如下所示:SqlConnection
Using
Dim strConn As String
strConn = "Server=(local);Database=dbAjout;Integrated Security=True"
Using MyConn As New SqlConnection(strConn)
Dim cmd As SqlCommand
Dim query As String = "EXEC CTable"
cmd = New SqlCommand(query, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery()
End Using
Note: The Using
statement can be applied to any object that implements the IDisposable
interface. In this case it will automatically close the SqlConnection
object by calling the Close
method for you. Behind the scenes, the VB.NET compiler will see the Using
statement and wrap the Using
block in a Try
block and give it a Finally
block, which is where the SqlConnection
's Close
method will be invoked on your behalf. This syntax makes the code easier to read in my opinion, easier to maintain as you can see everything that is contained within the connection object and removes the need to 'remember' to close the underlying database connection.
注意:该Using
语句可以应用于实现该IDisposable
接口的任何对象。在这种情况下,它将SqlConnection
通过Close
为您调用方法来自动关闭对象。在幕后,VB.NET 编译器将看到该Using
语句并将该Using
块包装在一个Try
块中并为其提供一个Finally
块,在该块中将代表您调用SqlConnection
的Close
方法。在我看来,这种语法使代码更易于阅读,更易于维护,因为您可以看到连接对象中包含的所有内容,并且无需“记住”关闭底层数据库连接。