1

When I use server explorer in Visual Studio and add a local DB on my D drive, I get a connection string and the connection test is successful.

But when I want to use that connection string like below to attach the database without the wizard and jut by code, I get an error on opening the connection, my connection string is provided below:

string coonection_string ="Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\\x\book.mdf;Integrated Security=True;Connect Timeout=30";

try
{
      SqlConnection myconnection = new SqlConnection(coonection_string);
      myconnection.Open();
      MessageBox.Show(" connected");
}
catch (Exception e1)
{
      MessageBox.Show(e1.ToString());
}
6
  • The first thing I would try is getting rid of the double back slash here: D:\\... Commented May 2, 2015 at 7:24
  • 2
    Also, can you show us the error message? Commented May 2, 2015 at 7:25
  • 2
    then what error did you get? Commented May 2, 2015 at 7:25
  • 3
    Can you please post the error that you are getting? Commented May 2, 2015 at 7:25
  • 1
    You are not helping yourself. Saying I have an error and not telling us what is this error is a bit useless. Now you can hope that someone has a crystall ball and can see your monitor from here... (or can you tell us that pesky error message?) By the way, it is MessageBox.Show(e1.Message) Commented May 2, 2015 at 7:37

3 Answers 3

1

Your connection string is wrong. Eigher mdf file in your local project or sqlexpres or you can use a database name in the connection string like

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True

or 

Data Source=(LocalDb)\v11.0;Initial Catalog=MyDatabase;Integrated Security=SSPI;

Check this link.

DB Connection string in Web.config to use attached .mdf database won't work

Always use Web.config file for connection string and access the entry in code as

Dim mWebSvr As String = ConfigurationSettings.AppSettings("Connectionstring")
Sign up to request clarification or add additional context in comments.

Comments

1

Try to put @ before connection string. We use @ before strings to avoid having to escape special characters.

string coonection_string =@"Data Source=(LocalDB)  \v11.0;AttachDbFilename=D:\\x\book.mdf;Integrated Security=True;Connect Timeout=30";

Comments

1

Keep an @ symbol in-front of the connection string,in C# backslash is a escape character

string coonection_string =@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\\x\book.mdf;Integrated Security=True;Connect Timeout=30";

else your connection string may not be in correct format

SqlConnectionStringBuilder.AttachDBFilename Property

2 Comments

So, using the @ for escaping what does @"D:\\x\book.mdf" become?
@Alex it needs to be change like this @"D:\x\book.mdf"

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.