2011-02-24 282 views
1

我已經使用這段代碼連接到數據庫..它顯示錯誤,如ConnectionString屬性尚未初始化。錯誤: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() 

任何建議?

回答

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 
+0

我給它就像它顯示像System.ArgumentException錯誤:關鍵字不支持:'連接超時'。 – bala3569 2011-02-24 12:35:22

+0

@ bala3569:你可以試試'Connection Timeout'或'Timeout' – 2011-02-24 13:35:12

+0

@ bala3569:我還注意到你沒有指定一個'Server'或'Data Source'。 – 2011-02-24 13:38:13

1

嗯,這是未初始化:

Dim Conn As New SqlConnection(sConnString) 
Conn.Open() 
+0

我給它就像它顯示像System.ArgumentException錯誤:關鍵字不支持:'連接\t超時'。 – bala3569 2011-02-24 12:25:08

+0

at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable的同義詞,布爾firstKey) – bala3569 2011-02-24 12:26:55

2

你宣佈你的連接字符串sConnString,卻忘了要通過連接字符串SqlConnection()

Dim Conn As New SqlConnection(sConnString) 
Conn.Open() 
+0

我給它就像它顯示像System.ArgumentException錯誤:關鍵字不支持:'連接超時' 。 – bala3569 2011-02-24 12:25:26

+0

at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable同義詞,布爾firstKey) – bala3569 2011-02-24 12:26:29

+0

@bala:它是'Connect Timeout',只有一個空格。 – BoltClock 2011-02-24 12:26:46

0

連接/重新

解決方案時的連接不宣蜇屬性錯誤總是會發生很簡單

dim con as new SqlConnection 
con.connectionString = "Provide your SQL connection" 
con.open 
'write code or action you want to perform 
con.close 

希望它能幫助。

相關問題