有人说sql语句中的参数前面的"@"作用是让程序把输入的字符当作纯文本字符串,因此可以防止sql注入攻击,是这样吗?
可我发现有的代码在定义普通字符串的时候在字符串前面也加一个"@"不知有什么作用?
谢谢!!

解决方案 »

  1.   

    对 string aa = @"a\b'c";
    同 string aa = "a\\b\'c";
      

  2.   

    1) string sql = "update table1 set field1 = @abc ";

    2) string tmp = @"D:\test\txt.txt";是不同的,两回事;2)用 @ 引起来的字符串以 @ 开头,里面就可以可以包括换码序列在内的任何字符,而不需要特殊的转义处理,优点是可以轻松写出任何字符串,例如文件路径;1)@ 符号的另一种用法是使用碰巧成为 C# 关键字的被引用的标识符,例如
     SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
       myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City";    
       myCommand.Parameters.Add("@city",textbox1.text);
    那么它就可以有效防止脚本注入,不管textbox1里面输入了什么
      

  3.   

    这是抄别人的回贴,呵呵~
    C# 支持两种形式的字符串数据符号:规则字符串数据符号和逐字的字符串数据符号。规则字符串数字符号由用双引号括起0或更多字符组成,例如"Hello, world",并且也许会包括简单转意序列(例如\t表示tab字符)和十六进制转意序列。
    逐字的字符串数据符号由一个@字符后面跟着双引号括起的0或者更多字符组成。一个简单的例子是@"Hello, world"。在一个逐字字符串数据符号中,分割符间的字符通常认为是逐字的,只有引用转意序列例外。特别的是,简单转意序列和十六进制转意序列在逐字字符串数据符号中不支持。一个逐字字符串数据符号可能会跨越很多行。例子
    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"
    string f = @"Joe said ""Hello"" to me";// Joe said "Hello"
    string g = "\\\\sever\\share\\file.txt";// \\server\share\file.txt
    string h = @"\\server\share\file.txt";// \\server\share\file.txt
    string i = "one\ntwo\nthree";
    string j = @"one
    two
    three";