sql connection.open()exception

我是.net中的新手,我正在使用Windows窗体应用程序。 我正在尝试将我的应用程序连接到基于Visual Studio服务的数据库。 我只是在提交按钮后面编写代码。

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的连接时发生与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。validation实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 – 无法打开与SQL Server的连接)“

请给我一个简单的答案和解决方案,因为我对这些事情不熟悉。

您在连接字符串中缺少\以转义\ 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");