2

我正在使用StringBuilder.Append()VB.NET 中的方法创建一个大型 SQL 语句。我想将整个 SQL 语句放在一个Append()块中,并使用某种功能自动创建许多Append()块来清理代码。

Append()我正在使用 Visual Studio 2008 和 ReSharper 7,但除了手动添加块外,我找不到任何方法。ReSharper 的代码清理出人意料地没有帮助。

这可能吗?

编辑进一步澄清:

我希望能够编写一个长的 Append() 并让 IDE 或某些插件将其分成几个 Appends() 以达到审美目的。

例如:

Dim sql As New StringBuilder

sql.Append("SELECT Condition1 AS ""THIS_NAME"", Condition2 AS ""THIS_OTHER_NAME"", Condition3 AS ""ANOTHER_NAME"", Condition4 AS ""ANOTHER_NAME"", Condition5 AS ""THIS_NAME"" FROM MY_TABLE WHERE Condition1 = 'ThisValue' AND Condition2 = 'ThisOtherValue' AND Condition3 = 'ThisOtherValue' AND Condition4 = 'AnotherValue' AND Condition5 = 'SomeOtherValue'")

上面的 Append() 很长,可读性不是很好。但是,将 SQL 语句拆分为多个 Append 块时,有时很难使所有语法都正确。我想要一些插件将其转换为以下内容:

sql.Append("SELECT Condition1 AS ""THIS_NAME"",")
sql.Append(" Condition2 AS ""THIS_OTHER_NAME"",")
sql.Append(" Condition3 AS ""ANOTHER_NAME"",")
sql.Append(" Condition4 AS ""ANOTHER_NAME"",")
sql.Append(" Condition5 AS ""THIS_NAME"",")
sql.Append(" FROM MY_TABLE"",")
sql.Append(" WHERE Condition1 = 'ThisValue'")

....等等.. 它不必以这种方式精确格式化,只需将其分成几个 Append 语句的分隔符。

4

2 回答 2

1

The answer to your question is: "No, there's no R# ability for this."

However if this is purely for formatting, a much more appropriate solution is to have your String literals be spread across multiple lines.

You don't want to pollute your code with a dozen method calls to achieve the same thing.

    sql.Append("SELECT Condition1 as ""THIS_NAME""," & _
               " Condition2 as ""THIS_OTHER_NAME""," & _
               " Condition3 AS ""ANOTHER_NAME"",")
于 2013-01-22T19:32:34.287 回答
0

我认为我不完全理解您的问题,但在这里您可以添加任意数量的 SQL 语句......

  Dim SQLBuilder As New System.Text.StringBuilder

   SQLBuilder.Append("YOUR SQL STATEMENT")            

现在我不确定你在清理代码中添加许多附加块是什么意思?您能否更详细地解释一下,以便我们为您提供帮助?

我想说如果你想清除 StringBuilder 你可以在再次填充之前清空它......

 SQLBuilder.Remove(0, SQLBuilder.Length)

如果你所做的只是创建一个语句,为什么不声明一个字符串变量来保存这个语句呢?见下文...

 Dim strSQLQuery As String = "YOUR STATEMENT"

然后你可以在你想要的地方使用它并随时设置它......

谢谢!

于 2013-01-16T22:44:09.093 回答