3

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.

3
  • 3
    a simplified answer would be, something is wrong with your connection string Commented Feb 12, 2014 at 18:46
  • 1
    incorrect connection string Commented Feb 12, 2014 at 18:46
  • Try using SqlLocalDB Utility technet.microsoft.com/en-us/library/hh212961.aspx Commented Feb 12, 2014 at 19:09

1 Answer 1

3

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.