1

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 declaring a stored procedure's parameters.

5
  • This makes me want to cry. .NET has great support for placeholders. Please use it. What you are looking for is verbatim literal strings -- see the examples.
    – user166390
    Commented Sep 2, 2011 at 19:19
  • It's a quoted string literal, it has nothing to do with sql or paramaters. msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx
    – Brook
    Commented Sep 2, 2011 at 19:19
  • 4
    @pst: If the OP is asking what its purpose is, it stands to reason s/he isn't the author of the code and is looking to understand what it's doing.
    – Yuck
    Commented Sep 2, 2011 at 19:22
  • @Yuck That is what the link is for, naturally.
    – user166390
    Commented Sep 2, 2011 at 19:23
  • Yes, I am trying to understand someone else's codes. Why would I be asking if I am the one who coded it in the first place. Thank you for stopping by nonetheless.
    – KTmercury
    Commented Sep 2, 2011 at 21:22

1 Answer 1

12

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 :-)

1
  • 1
    I want to thank the magical fairy of quotes, that added the title of the page I linked and a quote of the text! :-) It's always good to know there are good fairies that help you while you are helping someone else :-) :-)
    – xanatos
    Commented Sep 2, 2011 at 19:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.