2010-09-13 42 views
8

我正在使用Visual Studio 2008,而我的數據庫是SQL Server 2000.在服務器資源管理器中添加連接時出錯:「無法添加數據連接。ExecuteScalar需要一個開放且可用的連接。」

我想添加連接到VS中的服務器資源管理器。數據源是Microsoft SQL Server(SqlClient)。輸入我的所有信息後,我點擊測試連接,它是成功的。

但是當我點擊確定,我得到的錯誤:

無法添加數據連接。 ExecuteScalar需要一個開放且可用的連接。連接的當前狀態已關閉。

+0

重新啓動您的計算機。 – 2010-09-13 19:02:46

+0

那有效......背後的故事是什麼? – Steven 2010-09-13 19:13:07

+1

我不知道,只是一個建議。 – 2010-09-13 21:23:38

回答

11

重新啓動Visual Studio。然後,重新啓動您的電腦。

+7

重新啓用VS先,那應該夠了 – 2010-10-14 01:05:58

+0

對我重新啓動VS就夠了 – Regfor 2012-05-14 08:20:26

+0

同樣在這裏。重新啓動VS,一切正常。感謝名單! – 2015-02-20 16:42:31

0

您可以打開服務器資源管理器(查看 - >服務器資源管理器)重新連接該連接。

您可以刪除當前連接以再次打開同一個連接。

0

對於那些正在使用SQL命令並且出現錯誤「無法添加數據連接的錯誤,ExecuteScalar需要一個開放的可用連接,連接的當前狀態已關閉。試試這個:

 using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      using (SqlCommand comm = new SqlCommand()) 
      { 
       // query to select all the rows whose column name is the same as id 
       comm.CommandText = "SELECT COUNT(*) from tableName where colName like @val1"; 
       comm.Connection = conn; 
       conn.Open(); // <---- adding this line fixed the error for me 
       comm.Parameters.AddWithValue("@val1", id); 
       // retrieve how many rows are returned after executing the query 
       count = (int)comm.ExecuteScalar(); // < --- where the error originally occurred 
      } 
     } 
相關問題