“不允许更改”connecionstring属性“。”连接的当前状态是打开的“ [英] "Not allowd to change the"connecionstring property".The connection's current state is open "
本文介绍了“不允许更改”connecionstring属性“。”连接的当前状态是打开的“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好吧,
im试图运行我的代码,但它给我一个错误。不允许更改connectionstring属性。连接''当前状态是开放的如何解决这个错误?
以下是我的代码..
hi all,
i m trying to run my code but it give me an error."Not allowed to change the"connectionstring property".The connection''s current state is open " how to solve this error??
below is my code..
1 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5. Click
2 Try
3 Dim file_name As String = "E:\EXPORTDATA\customerInformation.txt"
4 Dim txtline, q, data(4) As String
5
6
7 If System.IO.File.Exists(file_name) = True Then
8 Dim objreader As New System.IO.StreamReader(file_name)
9 Do While objreader.Peek <> -1
10 ' Storing line to string variable
11 txtline = objreader.ReadLine() & vbNewLine
12 'breaking the line & stroing to array variable
13 data = txtline.Split(",")
14 q = "insert into customer_info values(@CUSTOMER_NAME,@CUSTOMER_ID,@CUSTOMER_ADD,@CITY)"
15 con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:E\connectivity\MYDB.accdb;"
16
17 con.Open()
18
19 cmd = New OleDbCommand(q, con)
20 cmd.Connection = con
21
22 With cmd.Parameters
23 .Add("@CUSTOMER_NAME", OleDbType.VarChar).Value = data(0)
24 .Add("@CUSTOMER_ID", OleDbType.VarChar).Value = data(1)
25 .Add("@CUSTOMER_ADD", OleDbType.VarChar).Value = data(2)
26 .Add("@CITY", OleDbType.VarChar).Value = data(3)
27
28 End With
29 cmd.ExecuteNonQuery()
30 con.Close()
31 Loop
32 End If
33
34 MessageBox.Show("Data Saved")
35
36 Catch ex As Exception
37 MsgBox(ex.Message.ToString)
38 End Try
39
40 End Sub
推荐答案
在第17行,您打开了与数据库的连接,在第20行,您想要更改ConnectionString。
交换这两行,应该这样做。
On line 17, you opened a connection to the database and on line 20, you want to change the ConnectionString.
Interchange these two line and that should do it.
你的变量 con
是在你项目的其他地方定义的,它'' s状态从另一个连接打开。仔细检查你编程的sql-connections,使用后没有关闭。
Your variable con
is defined somewhere else in your project, and it''s state is open from another connection. Double check you program for sql-connections which is not closed after use.
这篇关于“不允许更改”connecionstring属性“。”连接的当前状态是打开的“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文