我已经使用这段代码连接到数据库..它显示错误,如ConnectionString属性尚未初始化。
Dim sConnString As String = "user id=" + sUserID + ";password=" + sPwd + ";initial catalog=" + sDatabase + ";Connect Timeout = 30" + ";Pooling=true;Max Pool Size=" + MaxPoolSize + ";Min Pool Size=" + MinPoolSize + ";"
Dim Conn As New SqlConnection()
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO NEC_Customer_Mst(Customer_Code,Customer_Name) VALUES(Customercode,Customername)"
cmd.Connection = Conn
Conn.Open()
cmd.ExecuteNonQuery()
Conn.Close()
有什么建议吗?
答案 0 :(得分:2)
您声明了连接字符串sConnString
,但忘记将连接字符串传递给SqlConnection()
:
Dim Conn As New SqlConnection(sConnString)
Conn.Open()
答案 1 :(得分:2)
我会更改您的代码,以便您使用using
语句。这样它就会在资源完成后清理资源。
Dim sConnString As String = "user id=" + sUserID + ";password=" + sPwd + ";initial catalog=" + sDatabase + ";Connect Timeout=30;Pooling=true;Max Pool Size=" + MaxPoolSize + ";Min Pool Size=" + MinPoolSize + ";"
Using Conn As New SqlConnection(sConnString)
Using cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO NEC_Customer_Mst(Customer_Code,Customer_Name) VALUES(Customercode,Customername)"
cmd.Connection = Conn
Conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
答案 2 :(得分:1)
嗯,它没有初始化:
Dim Conn As New SqlConnection(sConnString)
Conn.Open()
答案 3 :(得分:0)
连接Sting属性错误始终在未声明/续订连接时发生
解决方案很简单
dim con as new SqlConnection
con.connectionString = "Provide your SQL connection"
con.open
'write code or action you want to perform
con.close
希望它有所帮助。