-2

我在 myconn.Open(); 行遇到错误 如何解决它。

 {"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:}


SqlConnection myconn = new SqlConnection();
        String connection = "Data Source=SQLEXPRESS;AttachDbFilename=C:\\Users\\Winer\\Documents\\Visual Studio 2008\\WebSites1\\App_Data\\Database.mdf";
        myconn.ConnectionString = connection;
        myconn.Open();

任何人都可以说一下吗?

4

3 回答 3

4

这只是意味着SQLEXPRESS找不到服务器。我认为你有一个拼写错误。如果可行,试试这个:(Data Source=.\SQLEXPRESS;

String connection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Winer\\Documents\\Visual Studio 2008\\WebSites1\\App_Data\\Database.mdf";

更新 1:

.意味着或等同于localhost

Data Source=.\SQLEXPRESS;是相同的Data Source=localhost\SQLEXPRESS;

或尝试

Data Source=YOURCOMPUTERNAME\SQLEXPRESS;

更新 2

为什么不使用这种连接字符串格式?

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

或者

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

在哪里

myServerAddress是您的服务器地址
myDataBase是您的数据库的名称
myUsername是用户名以及
myPassword您的密码。

此链接上的更多 ConnectionString 格式。

于 2012-06-18T05:43:38.190 回答
0

确保您的连接字符串有效。我倾向于向学生推荐这个:

  1. 在“服务器资源管理器”面板中,使用向导创建与数据库的连接。
  2. On the new connection, in the 'Data Connections' section, right-click on the connection to display the context menu.
  3. Select 'Properties'. Copy and paste the 'Connection String' value in place of your existing string.

There are better ways to do this, but this is relatively simple, I think. HTH.

于 2012-06-18T05:48:47.460 回答
0
SqlConnection myconn = new SqlConnection();         
String connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Winer\Documents\Visual Studio 2008\WebSites1\App_Data\Database.mdf";         
myconn.ConnectionString = connection;         
myconn.Open(); 
于 2012-06-18T06:27:23.480 回答