我有这个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()
当我运行代码时,我在行conn3.Open()
上收到错误说:
连接时(state = Open),不允许更改'ConnectionString'属性。
它适用于第一个循环,但当我到达第二个循环时,它会停止并显示此错误
答案 0 :(得分:1)
在修改之前,您可以添加一些额外的检查:
If conn3.State = ConnectionState.Open Then
conn3.Close()
End If
答案 1 :(得分:1)
问题在于此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