问题描述
我正在从 Visual Studio 2008 的sql server 2008 管理版"中创建到我的数据库的连接.
i am creating a connection to my database in 'sql server 2008 management edition ' from visual studio 2008.
我在我的数据库中存储了一个名为CTable"的过程,我想执行它.(来自 Visual Studio)
i stored a procedure called "CTable" in my database and i want to execute it.(from visual studio)
这是我的代码:
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
但我有这个错误:在 System.Data.dll 中发生了类型为System.InvalidOperationException"的未处理异常
附加信息:连接未关闭.连接的当前状态是打开的."
Additional information: The connection was not closed. The connection's current state is open."
你有什么建议???我被屏蔽了,已经 4 小时了!!!!
what do you suggest???im blocked, its been 4 hours !!!!
推荐答案
您打开了两次连接.根据文档(SqlConnection.Open()
):
You open your connection twice. As per documentation (SqlConnection.Open()
):
例外
无效操作异常
- 无法在不指定数据源或服务器的情况下打开连接.
- 或
- 连接已打开.
这(可能)是导致 InvalidOperationException
被抛出的原因.
That is (probably) what causes the InvalidOperationException
to be thrown.
这篇关于System.Data.dll 中发生类型为“System.InvalidOperationException"的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!