错误{“发生与网络相关或特定于实例的错误..”}

时间:2012-06-18 05:40:20

标签: c# asp.net

我在线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();

任何人都能说出来吗?

3 个答案:

答案 0 :(得分: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;

OR

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

WHERE

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

<强> More ConnectionString format on this link.

答案 1 :(得分:0)

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

  1. 在“服务器资源管理器”面板中,使用向导创建与的连接 数据库。
  2. 在新连接上,在“数据连接”部分中,右键单击连接以显示上下文菜单。
  3. 选择“属性”。复制并粘贴“连接字符串”值 现有字符串的位置。
  4. 有更好的方法可以做到这一点,但我认为这相对简单。 HTH。

答案 2 :(得分: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();