Why is the address sign @ used in building SQL command?
Possible Duplicate:
What's the @ in front of a string for .NET?
这个SQL命令中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | static DataTable StaffsGetProjectIDHRCoordinators(SqlConnection conn, string targetDatabase) { try { SqlCommand cmd = new SqlCommand(@" SELECT DISTINCT Staff.* FROM Staff INNER JOIN" + targetDatabase.Replace("'","''") + @".dbo.ProjectHRCoordinator ProjectHRCoordinator ON Staff.StaffID = ProjectHRCoordinator.HRCoordinatorStaffID ORDER BY Staff.FName, Staff.LName", conn); return ExecuteDataTable(cmd); } finally { if (conn.State != ConnectionState.Closed) conn.Close(); } } |
在声明存储过程的参数时,我只熟悉
它表示该字符串是逐字字符串。
通常,要有一个包含
这在C规范的2.4.4.5字符串文字中介绍:
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.
< /块引用>
我要补充一点,在您引用的SQL块中,它可以在多行中编写脚本,而无需关闭字符串的每一行。如此的
1
2
3 string str ="A" +
"B" +
"C";你可以写
1
2
3 string str = @"A
B
C";注意这两个字符串是非常不同的!第二个包含行尾和对齐空间,但在SQL中这不是问题(在SQL中的许多地方都忽略了空白空间)。
您有可能在一行中编写了SQL命令。那么,
@ 是完全无用的,但许多程序员采用了一种"比对不起更安全"的方法,并将其随意地撒上,特别是当他们从其他可能包含\ escapes 的地方粘贴文本时。