2013-03-06 190 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/

希望它可能会有所帮助。