2

When writing Sql in VB.NET one often ends up with something quite unreadable due to VB's lack of multi-line strings.

For Example:

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"

You could break the string up using line-continuation characters, but the extra quote marks and line-continuation characters make this harder to read. Also it makes transferring the query between code and SSMS difficult.

Is there a solution that makes SQL readable within VB and also allows easy transfer of queries (via copy/paste) between VB code and SSMS (or any other SQL editor/IDE)?

Sam Axe
  • 33,313
  • 9
  • 55
  • 89

3 Answers3

3

The best solution I have found is to use VB's XML literals feature (available since VS 2008).
XML literal attributes allow for multi-line strings.

Dim sql As String =
    <sql text="
        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
    " />.Attribute("text").Value

The one caveat is that greater-than and less-than comparisons need to be encoded as XML entities: < becomes &lt; and > becomes &gt;.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • With Visual Studio 2015, this technique becomes an obsolete workaround, useful only for backward compatibility with users stuck at older Visual Basic. No longer "the best solution". :) A candidate for refactoring, whenever this is used. – miroxlav Jul 30 '15 at 02:36
  • 1
    @miroxlav Can you explain that. The VS 2015 feature allows multi-line strings, but it inserts newline characters at the end of each line. – Blackwood Jul 30 '15 at 03:06
  • @Blackwood - should we ask "why newlines are present" also with C# `@"Multi-line string"`? Do you think there is any reason why VB equivalent of these multi-liners should behave differently? In both VB and C# you can always remove newlines by adding `.Replace(...)`. – miroxlav Jul 30 '15 at 03:27
  • @miroxlav I'm not saying that the spurious newlines are a problem when they are inserted in a SQL command (which is what this question is about). I just think it's worth pointing out that they are inserted, which means the result is not the same as the old string concatenation and line continuation approach. – Blackwood Jul 30 '15 at 03:31
  • @Blackwood - sure. We are discussing below the answer showing XML literal - and there is no difference in newlines, XML literals keep them, too. So transition from obsolete XML workaround to new native form means 1:1 replacement with removing the XML oddities like `&` and `<` and trailing `.Attribute("text").Value` – miroxlav Jul 30 '15 at 03:46
  • @miroxlav I ran Sam's code and couldn't find a `vbCr` or `vbLf` in the string produced from the text attribute. – Blackwood Jul 30 '15 at 03:52
  • Yes because he uses `` instead of common workaround `text`. Good point. Looks like another reason to move away from these XML workarounds with side effects which are not obvious. BTW is there any reason why SQL should be sent to server with long sections of spaces preserved, but newlines removed? In SQL debugger, such an SQL will look buggy. This is another reason why Sam's answer is not the best one as he states. If using SQL in XML, then going with `text.Value` is much better. – miroxlav Jul 30 '15 at 03:58
3

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.)

Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99
  • Not everyone will be able to update to VS2015. But yes, the new multi-line strings and string interpolation features are a better solution than the XML literals, if one can indeed update to 2015. Given that internal update cycles rarely coincide with product releases I would hesitate to call the XML literal solution deprecated. Even if one is able to update to VS2015 one may not be able to target features available in VB 14. Not all team members can update at the same time and some clients may insist on previous versions of the compiler.. etc. Deprecated it is not. – Sam Axe Jul 30 '15 at 04:45
  • @SamAxe – hopefully everyone in your organization was able to upgrade already and finally we can safely call the XML literal solution deprecated. – miroxlav Dec 02 '19 at 14:31
0

I toggle word wrap on and off.

Edit>Advanced>Word wrap

I found it worth adding it as a button to the Visual Studio toolbar.

SSS
  • 4,807
  • 1
  • 23
  • 44