How to write readable SQL in VB.NET

主宰稳场 提交于 2019-12-06 09:17:18

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

miroxlav

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!