小编典典

sql connection.open()异常

sql

我是.net的新手,因此我正在Windows窗体应用程序上工作。我试图将我的应用程序连接到基于Visual Studio
Service的数据库。我只是在“提交”按钮后面编写代码。

private void button1_Click(object sender, EventArgs e)
{
     SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\mudasir\\Documents\\Visual Studio 2012\\Projects\\Medical_Store_System\\Medical_Store_System\\MSS_database.mdf;Integrated Security=True");
      con.Open();
      if (con.State == ConnectionState.Open)
      {
          textBox1.Text = "Congrats";
      }
      else
          textBox1.Text = "Sorry";
          con.Close();
}

在con.open();上 我遇到了一个异常,显示

“建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。找不到服务器或无法访问该服务器。请验证实例名称正确并且将SQL
Server配置为允许远程连接。(提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)”

请给我一个简化的答案和解决方案,因为我是这些事物的新手。


阅读 195

收藏
2021-03-08

共1个答案

小编典典

\在连接字符串中缺少来逃避\for (LocalDB)\version。所以像这样更新它。

SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\mudasir\\Documents\\Visual Studio 2012\\Projects\\Medical_Store_System\\Medical_Store_System\\MSS_database.mdf;Integrated Security=True");
2021-03-08