Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection String in C#.Net

//SQL PART

Line 1 : string dd = "Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\HEX\Documents\Visual Studio 2008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True";

Line 2 :SqlConnection sqlconobj = new SqlConnection(dd);

Line 3: sqlconobj.Open();

---------Errors Output------------

Unexpected character'\'

like image 406
Pijush Gupta Avatar asked Dec 01 '25 12:12

Pijush Gupta


1 Answers

In C# the backslash character has special meaning.
You need to double it or prefix your entire string with the Verbatim character @
And there is no need to put a double quote before and after the file name.
Formatting rules (the ending semicolon) allows spaces in path or file name for the AttachDbFileName

 string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + 
             @"C:\Users\HEX\Documents\Visual Studio 2008\" + 
             @"Projects\cventry_address_book_0.1\cventry_address_book_0.1" + 
             @"\addressbook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
like image 108
Steve Avatar answered Dec 04 '25 01:12

Steve