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

后端 未结 3 1945
你的背包
你的背包 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:29

    You can prefix a string constant with @ to span multiple lines. The only thing that would need to be escaped is quotes (using the double quote).

    string SQL = @"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)";
    
    0 讨论(0)
  • 2021-01-13 00:30

    First – Use multiline string literals like

    String sql = @"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)"
    
    • note that all line breaks inside the string and whitespace at the beginning of each line is retained

     
    Second – Let string interpolation work for you if you need any parametrization of string:

    String databaseName = "test"
    String tableName = "BarFoo"
    
    String sql = @"Use \{databaseName}
    alter table \{tablaName} alter column CouponName nvarchar(328)"
    
    • no more puzzles like String.Format("Use {0}; alter table {1}", databaseName, tableName)

     
    Third – Visual Basic already has multiline string literals, too. No more XML workarounds please.
    In VB, use

    Dim sql As String = "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)"
    
    'and with interpolated strings:
    Dim when As String = "since Visual Studio 2015"
    Dim note As String = $"String interpolation works {when}, see this VB-specific syntax!"
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题