简体   繁体   English

如何在VB.NET中编写可读的SQL

[英]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. 当在VB.NET中编写Sql时,由于VB缺少多行字符串,通常会导致一些难以理解的事情。

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. 这也使得在代码和SSMS之间传输查询变得困难。

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)? 有没有一种解决方案可以使SQL在VB中可读,还可以轻松地在VB代码和SSMS(或任何其他SQL编辑器/ IDE)之间传输查询(通过复制/粘贴)?

The best solution I have found is to use VB's XML literals feature (available since VS 2008). 我发现最好的解决方案是使用VB的XML文字功能(自VS 2008开始可用)。
XML literal attributes allow for multi-line strings. XML文字属性允许多行字符串。

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; 一个需要注意的是,大于和小于比较需要被编码为XML实体: <变得&lt; and > becomes &gt; 并且>变为&gt; .

Is it possible for you to upgrade to Visual Studio 2015 ? 您可以升级到Visual Studio 2015吗?
You can use new multi-line string literals in Visual Basic 14 : 您可以在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 < , & , ...). XML多重划线以前没有已知的限制(带有<& ,...的问题)。 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. 插值字符串内部的表达式也完全支持变量重命名,因此将tableName重命名为mainTableName还将在字符串内部执行重命名。

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 . 但是在T-SQL中, 它们只有一个特定的用途

More information: 1 , 2 更多信息: 12


Notice: If you wish to temporarily keep using deprecated XML workaround, then DON'T use form 注意:如果您希望暂时继续使用不推荐使用的XML解决方法,则不要使用表格
<tag attribute="text" />.Attribute("attribute").Value
because it removes new line characters what leads to strange SQL single-liners like 因为它删除了换行符 ,导致出现奇怪的SQL单行代码,例如
SELECT t1.Name, t1.Description, t2.Date FROM Users t1 . 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命令处理器的输入处)。 SQL readability is the main goal of this Q/A and this detail is part of it. SQL可读性是此Q / A的主要目标,并且此详细信息是其中的一部分。

(But remember this improved form of SQL in XML is deprecated, too = it works, but abandon it as soon as possible.) (但是请记住,XML中这种改进的SQL形式也已被弃用=它可以工作,但请尽快放弃。)

I toggle word wrap on and off. 我打开和关闭自动换行。

Edit > Advanced > Word wrap Edit > Advanced >自动Word wrap

I found it worth adding it as a button to the Visual Studio toolbar. 我发现值得将它作为按钮添加到Visual Studio工具栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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