// SQL PART
第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";
第2行:SqlConnection sqlconobj = new SqlConnection(dd);
第3行:sqlconobj.Open();
--------- 错误输出 ------------
意外字符'\'
在C#中,反斜杠字符具有特殊含义。您需要将其加倍或在整个字符串前加上Verbatim字符@并且无需在文件名前后加上双引号。格式设置规则(以分号结尾)在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";
尝试:
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";
您需要使用@
字符对字符串进行转义。或者,您可以将单个\
替换为\\
。
您应通过在字符串前面加上@
字符来对其进行转义。另外,您还应该在using语句中包装SqlConnection实例:
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();
}
您需要转义\
字符。使用这个:
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";
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";
这应该是您的查询连接字符串
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";