2013-03-06 213 views
2

我正在使用SQL Server。我曾經通過所有連接:SQL Server的連接字符串?

SqlConnection con = new SqlConnection("data source = .; database = xyzDatabase; integrated security = true");//developed with developer edition. 

現在我想運行我的SQL Server Express應用程序,所以我用這個:

SqlConnection con = new SqlConnection("data source = .\\sqlexpress; initial catalog = xyzDatabase; user id = sa; password = 123"); 

一點要記住的是,我在運行時創建我xyzDatabase形成加載事件。但是當我執行程序時,出現以下錯誤:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (.Net SqlClient Data Provider)

回答

0

您必須在sql express版本中激活sa用戶。

SqlConnection cn = new SqlConnection("datasource=.\sqlexpress; initialcatalog=yzDatabase; userid=sa;password=123"); 
+0

不** ** EVER **使用'sa'帳戶 - ** N-E-V-E-R **!要麼使用集成安全性,要麼爲你的應用程序使用**明確的**用戶。 – 2013-03-06 09:57:10

+0

與mssql ide,您可以登錄並更改sqlexpress的安全性。 – 2013-03-06 10:03:36

0

導入的命名空間

使用System.Data.SqlClient的(如果使用C#) 或 進口System.Data.SqlClient的(如果使用VB#)

您的SQL連接對象與連接會是這樣的:

SqlConnection con = new SqlConnection("Data Source=.;Database=xyzDatabase;User Id=sa;[email protected];"); 

如果仍然沒有解決您的錯誤,則可能是安裝不正確的問題o f MS-SQL SERVER實例。

http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/

希望它可能會有所幫助。