[英]Connection String in C#.Net
// SQL PART // SQL 部分
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";
第 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);
第 2 行: SqlConnection sqlconobj = new SqlConnection(dd);
Line 3 : sqlconobj.Open();
第 3 行: sqlconobj.Open();
--------- Errors Output ------------ ---------错误输出-----
Unexpected character'\\'意外字符'\\'
In C# the backslash character has special meaning.在 C# 中,反斜杠字符具有特殊含义。
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格式规则(结尾分号)允许在路径或文件名中为 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:此外,您应该将 SqlConnection 实例包装在 using 语句中:
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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.