views:

33

answers:

1

Suppose we want to select the data from the database then we write the query for that .

Example:

SqlConnection con=new SqlConnection(Connetion name)
string selectPkId = @"SELECT PK_ID FROM TABLE"
SqlCommand cmd=new SqlCommand(selectPkId ,con);

So,my question is that why we basically use @ before the sql query.If I don't use @ before that then it again work fine (does not give any error), then what is need of using "@" ? Please tell me.

+4  A: 

It's a verbatim string. This means newlines are preserved and escape sequences ignored:

string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world
string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
string h = @"\\server\share\file.txt";      // \\server\share\file.txt

MSDN Reference: String Literals

This comes in handy when you want escape sequences to be preserved without having to double escape them, eg:

string sql = @"UPDATE table SET comment='Hello\nWorld'"
// equivalent to:
string sql = "UPDATE table SET comment='Hello\\nWorld'"

Of course this is a very simple example, but most of the times it will give you a more readable string.

NullUserException