// 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'\\'
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";
Try:
string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\HEX\Documents\Visual Studio008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True";
You need to escape the string by using the @
character. Alternative you could replace single \\
's with \\\\
.
You should escape the string by prefixing it with the @
character. Also you should wrap your SqlConnection instance in a using statement:
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";
using (SqlConnection sqlconobj = new SqlConnection(dd))
{
sqlconobj.Open();
}
You need escape the \\
character. Use this:
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";
More: String literals
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";
This should be your query connection string
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";
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.