6

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

Dim SQL As String = <a> 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) </a>.Value

But cant seem to find its equivalent for C# Any suggestions?

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
tastydew
  • 677
  • 8
  • 21
  • Are you asking how to write a string constant that spans many lines in C#? – Mike Christensen Aug 06 '15 at 20:28
  • 1
    It is unclear what are you trying to do. What does it mean `.Value`? – Steve Aug 06 '15 at 20:29
  • I apologize if it appears as a stupid question. I am learning C# and yes, I was looking to write a string that spans many lines as I have very large blocks of SQL code that need to go into the code. In VB.net this would compile with no issues and run just fine. – tastydew Aug 06 '15 at 20:31
  • 1
    Verbatim string literals start with `@`. Like `string regex = @"[\w]*";` [More on Verbatim strings in C#](http://stackoverflow.com/questions/3311988/what-is-the-difference-between-a-regular-string-and-a-verbatim-string) – ryanyuyu Aug 06 '15 at 20:32
  • 1
    @The_Black_Smurf: It sure looks like valid VB.NET to me. The SQL looks pretty valid too. – Sam Axe Aug 06 '15 at 20:32
  • 6
    You're using (abusing) inline XML, which VB supports and C# does not. As for C#, @Eser has you on the right track. Use `string SQL = @"your multiline text here";` – Anthony Pegram Aug 06 '15 at 20:32
  • Ah, that seems to do the trick! thank you! Not sure why the downvotes? – tastydew Aug 06 '15 at 20:33
  • 5
    `+1` from me! I like this question because it taught a bunch of downvote trigger happy people something they didn't know about VB.NET.. – Mike Christensen Aug 06 '15 at 20:39
  • Appears so! Is there a reason why C# does not support inline XML? – tastydew Aug 06 '15 at 20:40
  • 1
    Different languages support different features, you could just as easily ask why Ruby doesn't support some random Python feature, or why some Go feature isn't available in JavaScript. Just use the language that lets you get the job done in the best way possible. – Mike Christensen Aug 06 '15 at 20:44
  • 2
    Quote from Eric Lippert: "*I am asked "why doesn't C# implement feature X?" all the time. The answer is always the same: because no one ever designed, specified, implemented, tested, documented and shipped that feature. All six of those things are necessary to make a feature happen. All of them cost huge amounts of time, effort and money. Features are not cheap, and we try very hard to make sure that we are only shipping those features which give the best possible benefits to our users given our constrained time, effort and money budgets.*" – Mike Christensen Aug 06 '15 at 20:46

3 Answers3

9

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)";
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
1

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.

Community
  • 1
  • 1
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
1

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!"
miroxlav
  • 11,796
  • 5
  • 58
  • 99