C#中的@

 

 

using System.Data.SqlClient;
using System.Data;
using System;
class MyClass
{

    
void Test()
    
{
        
// 1 加在字符串前面,字符串中的  失去轉義符的作用,直接寫字符串而不需要考慮轉義字符
        string path = @"C:Windows"// 如果不加 @,編譯會提示無法識別的轉義序列

        
// 如果不加 @,可以寫成如下
        string path2 = "C:/Windows/";

        
// 2 加在字符串前面,字符串中的 " 要用 "" 表示
        string str = @"aaa=""bbb""";
        
// 不加 @,可以寫成
        string str2 = "aaa="bbb"";

        
// 3 加在字符串前面,換行空格都保存着,方便閱讀代碼
        string insert = @"
            insert into Users
            (
                UserID,
                Username,
                Email
            ) values
            (
                @UserID,
                @Username,
                @Email
            )
";


        
// 4 用關鍵字做變量時在關鍵字前面加@ 
        string @operator = "+";
        
string @class = "分類一";

        Console.WriteLine(@operator);
        Console.WriteLine(@class);


        
// 5 作爲sql語句裏的一個“標籤”,聲明此處需要插入一個參數 
        string delete = "delete from Categery where CategoryID=@CategoryID";
        SqlConnection connection 
= new SqlConnection("connectionString");
        SqlCommand command 
= new SqlCommand(delete, connection);
        command.Parameters.Add(
"@CategoryID", SqlDbType.BigInt);

    }
 // Test()

}
// class MyClass

 本文轉自:http://www.chenjiliang.com/Article/View.aspx?ArticleID=207&TypeID=34

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章