Is it possible for you to upgrade to Visual Studio 2015?
You can use new multi-line string literals in Visual Basic 14:
Dim sql As String = "
SELECT t1.Name,
t1.Description,
t1.Address,
t1.PhoneNumber,
t2.RegistrationID,
t2.Date,
t2.Description,
t2.RegistrationStatus
FROM Users t1
JOIN Registrations t2 ON t1.UserID = t2.UserID
WHERE t2.RegistrationID = @RegistrationID
"
There are no limitations previously known from XML multi-liners (problems with <
, &
, ...). The only thing you need to escape inside the string is "
(replace it with ""
).
And great new string interpolation feature helps you make your strings readable like never before:
Dim tableName As String = "Registrations"
Dim currentOrderByColumn As String = "t2.Date"
Dim sql = $"SELECT t1.Name, t1.Description FROM {tableName} ORDER BY {currentOrderByColumn}"
Dim sql2 = $"
SELECT t1.Name, t1.Description
FROM {tableName}
ORDER BY {currentOrderByColumn}
"
Expressions inside interpolated strings also fully support variable renaming, so renaming tableName
to mainTableName
will also perform renaming inside the string.
Additional characters you need to take care of in this type of string are {
and }
- you must replace them with {{
or }}
respectively. But in T-SQL they have only one specific purpose.
More information: 1, 2
Notice: If you wish to temporarily keep using deprecated XML workaround, then DON'T use form
<tag attribute="text" />.Attribute("attribute").Value
because it removes new line characters what leads to strange SQL single-liners like
SELECT t1.Name, t1.Description, t2.Date FROM Users t1
.
Instead, use form
<tag>text</tag>.Value
which preserves line endings so what you see (in code editor) is what you get (at input of SQL command processor). SQL readability is the main goal of this Q/A and this detail is part of it.
(But remember this improved form of SQL in XML is deprecated, too = it works, but abandon it as soon as possible.)