简体   繁体   中英

How to write readable SQL in VB.NET

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM