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
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)";
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)"
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)"
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!"
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.