简体   繁体   中英

sql connection.open() exception

I am new in .net and in this i am working on windows form application. I am trying to connect my application to visual studio service based database. I am simply writing a code behind submit button.

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();
}

On con.open(); i met with an exception that shows

"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)"

Please give me a simplified answer and solution because i am new to these things.

You are missing a \\ in the connection string to escape the \\ for (LocalDB)\\version . So update it like so.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM