Possible Duplicate:
What's the @ in front of a string for .NET?
What is that @
in this SQL command doing? Would its first and second occurrence serve the same purpose?
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();
}
}
I'm just familiar with the @
signs when declar开发者_如何学JAVAing a stored procedure's parameters.
It means the string is a verbatim string literal. The \
won't be treated in a special way. \n
isn't new line, it's slash and n. Very useful for paths.
Normally to have a string containing C:\Windows\myfile.txt
you would need to write "C:\\Windows\\myfile.txt"
OR with verbatim string literals @"C:\Windows\myfile.txt"
.
This is covered in 2.4.4.5 String literals of the C# specification:
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.
I'll add that in the block of SQL you quoted it is used to be able to write the script in multiple lines without closing every line the string. So insted of
string str = "A " +
"B " +
"C";
you can write
string str = @"A
B
C";
Be aware that the two strings are very different! The second one contains the end-of-lines and the alignment spaces, but in SQL this isn't a problem (empty space is ignored in many places in SQL).
There is a chance that you wrote you SQL command in a single line. Then the @
was totally useless, but many programmers adopt a "better safe than sorry" and sprinkle it quite liberally, especially when they are pasting text from somewhere else that could contain \ escapes
:-)
精彩评论