Ali Arghianey
Ali Arghianey

Reputation: 11

SQL connection string for local db

I want to connect to database with this connection string

SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\4L46D\Documents\Visual Studio 2013\Projects\create table\create table\Database1.mdf";Integrated Security=True");

but I get the following error: get red line on address of database file:

AttachDbFilename="C:\Users\4L46D\Documents\Visual Studio 2013\Projects\create table\create table\Database1.mdf

enter image description here

Could you please help to how write the connection string?

solution : use @ before first double quotation(") and delete " from database path . use like this :

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\4L46D\Documents\Visual Studio 2013\Projects\create table\create table\Database1.mdf;Integrated Security=True");

Upvotes: 1

Views: 1090

Answers (1)

Hammad Shabbir
Hammad Shabbir

Reputation: 731

To use inverted commas with in string. you need to escape character with backslash i.e (\)

SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=\"C:\\Users\\4L46D\\Documents\\Visual Studio 2013\\Projects\\create table\\create table\\Database1.mdf\";Integrated Security=True");


Upvotes: 1

Related Questions