2011-02-24 310 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 

希望它能帮助。

相关问题