2012-07-16 28 views
1

林面臨着在Windows應用程序數據庫連接C#數據庫連接的問題...這裏是我的連接,,, PC2是PCNAmeWindows應用程序的數據庫連接C#

private static SqlConnection con = new SqlConnection(
    "Data Source=PC2\\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=True" 
); 

當我運行的形式,我得到這個未處理的異常on con.Open();

SQLEXCEPTION被unhandeled:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

希望有人能幫助我試着去解決它幾個小時,但沒有工作。 MSDN上

+5

[請確保您的數據庫引擎已配置爲接受遠程連接](http://www.sswug.org/articlesection/default.aspx?TargetID=44331) – Habib 2012-07-16 11:06:56

+2

您配置了SQLExpress接受遠程連接嗎? http://blogs.msdn.com/b/sqlexpress/archive/2005/05/05/415084.aspx – 2012-07-16 11:07:29

+0

1.您是否安裝了sql server,2.您可以使用sqlcmd登錄嗎? 3.是運行的sql服務器 – 2012-07-16 11:07:37

回答

0

入住這排查步驟:

The SQL Server client cannot connect to the server MSDN

如果您在連接字符串分隔到您的ado.net SQL連接好得多。

private const string _thisConnectionString = "Data Source=PC2\\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=SSPI"; 

使用「使用」聲明確保您始終關閉連接。

Best Practices for Using ADO.NET MSDN

 using (var conn = new SqlConnection(_thisConnectionString)) 
     { 
      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandText = "Your SQL Query STuff"; 

      conn.Open(); 

     }   

問候

0

試試這個:

private static SqlConnection con = new SqlConnection(
    "Data Source=PC2\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=True" 
); 
0

轉到SQL Server配置管理 - >點擊SQL Server服務的

驗證SQL Server和SQL Server瀏覽器都在運行。你可以在那裏看到貪婪的播放按鈕。如果沒有右鍵單擊顯示紅色的服務,請轉到屬性,單擊內置帳戶,然後按應用然後確定,然後嘗試通過右鍵單擊來啓動。

相關問題