vb.net 不允许更改 ConnectionString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26604622/
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
Not allowed to change the ConnectionString
提问by user3843997
I have this vb.net code:
我有这个 vb.net 代码:
conn6.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "
conn6.Open()
SQL = "select calltype from billing group by calltype "
myCommand6.Connection = conn6
myCommand6.CommandText = SQL
reader6 = myCommand6.ExecuteReader
While reader6.Read
cdr_call_type = reader6.GetString(0)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'do VoIP bit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If customerid_voip <> "" Or customerid_voip_new <> "" Then
'do customer VoIP Bit, which means getting the latest value from billing table
TextBox1.Text = "Got VoIP - " + cdr_call_type + vbCrLf + TextBox1.Text
Me.Refresh()
conn3.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "
conn3.Open()
SQL = "select reseller_bill, customer_bill, phone, calltype, timestamp, seconds from billing where calltype = '" + reader6.GetString(0) + "' AND (source='CDR' or source='' or source='VOIP') and (company='" + customerid_voip + "' or company='" + customerid_voip_new + "') "
myCommand3.Connection = conn3
myCommand3.CommandText = SQL
reader3 = myCommand3.ExecuteReader
While reader3.Read
'do stuff here
End While
reader3.Close()
conn3.Close()
End If
End While
reader6.Close()
conn6.Close()
When I run the code, I get an error on line conn3.Open()saying:
当我运行代码时,我在网上收到一条错误消息conn3.Open():
Not allowed to change the 'ConnectionString' property while the connection (state=Open).
不允许在连接时更改“ConnectionString”属性(状态 = 打开)。
It works on the first loop, but when I get around to the second loop it stops and displays this error
它适用于第一个循环,但是当我进入第二个循环时它会停止并显示此错误
回答by Baby
You may add some additional checking, before modifying it:
在修改之前,您可以添加一些额外的检查:
If conn3.State = ConnectionState.Open Then
conn3.Close()
End If
回答by Baby
the problem is here in this while loopyou are trying to change the connectionString of an already defined connection, it can be avoided by creating a new connection each time within the loop as follows:
问题就在这里,while loop您正在尝试更改已定义连接的 connectionString,可以通过每次在循环内创建一个新连接来避免这种情况,如下所示:
While reader6.Read
cdr_call_type = reader6.GetString(0)
If customerid_voip <> "" Or customerid_voip_new <> "" Then
TextBox1.Text = "Got VoIP - " + cdr_call_type + vbCrLf + TextBox1.Text
Me.Refresh()
Dim conn3 As New OdbcConnection '<--- declaring a new connection
'<---- in each iteration connection is treated as a new one
'<---- so initializing is not became a problem
conn3.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "
conn3.Open()
'<------
' Remaining codes comes here
'<------
End IF

