2014-10-28 115 views
-1

我有這樣的vb.net代碼:不允許改變的ConnectionString

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()一個錯誤說:

不允許更改'ConnectionString'屬性,而連接(state = Open)。

它可以在第一個循環,但是當我避開第二循環停止並顯示此錯誤

+2

錯誤消息指出在打開連接字符串時無法更改連接字符串。所以你只需要在修改它之前關閉它。 – 2014-10-28 09:22:57

+0

我已經關閉它,使用conn3.close() – user3843997 2014-10-28 09:23:46

+0

關閉conn6在嘗試使用另一個連接之前 – NoviceProgrammer 2014-10-28 09:24:46

回答

1

問題出在這裏while loop你試圖改變一個已經定義的連接的連接字符串,它可以通過每次在循環內創建一個新連接來避免,如下所示:

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 
1

您可以添加一些額外的檢查,修改前:

If conn3.State = ConnectionState.Open Then 
    conn3.Close() 
End If