How to achieve multi-line strings in C#; an alternative to VB's XML Literals?

后端 未结 3 1946
你的背包
你的背包 2021-01-13 00:03

I had a project in vb.net that used XML Literals to process large blocks of SQL like so:

Dim SQL As String =  Use test

alter table BarFoo alter col         


        
3条回答
  •  鱼传尺愫
    2021-01-13 00:42

    It seems you are trying to mimic the behavior of a verbatim string in C#. These literals start with a leadin @ before the string to let the compiler to not process character escapes. The only character escape you need is to escape a " with "" instead.

    So your query would look like

    string query = @"
    Use test
    
    alter table BarFoo alter column CouponName nvarchar(328)
    alter table Foo alter column IngredientName nvarchar(328)
    alter table Bar alter column IngredientShortDescription nvarchar(328)
    alter table FooBar alter column ItemName nvarchar(328)";
    

    This includes the newline characters \n as well as any leading spacing. In SQL whitespace is just for readability anyway, but it's good to remember that the white space is completely preserved with these literals.

提交回复
热议问题