MSSQL到VB.NET的转换

时间:2015-05-19 10:52:20

标签: sql-server vb.net command

我正在开发一个将T-SQL程序转换为VB.NET代码的项目,我一直在思考如何在VB.NET中编写一些整洁,干净的东西。

例如,这是T-SQL:

insert into X(
 column1, 
 column2, 
 ....., 
 column100) 
select 
 column1, 
 column2, 
 ....., 
 column100 
 from Y 

我应该如何将此语句转换为VB.NET?如果我使用字符串构建器,它将类似于:

Dim sb As New StringBuilder()
With sb
    .Append("Insert into x(")
    .Append("column1,")
    .Append("column2,")
    .Append("...,")
    ......
End With

代码太长了。还有更好的选择吗?

2 个答案:

答案 0 :(得分:0)

对不起,我没有声誉将此作为"评论"但根据我的经验,LINQ帮助我缩短了SQL程序。

https://msdn.microsoft.com/en-us/library/bb386946(v=vs.110).aspx

答案 1 :(得分:0)

由于遗憾的是,在C#中没有逐字字符串文字可以写下这个:

string insertSQL = @"
insert into X(
 column1, 
 column2, 
 ....., 
 column100) 
select 
 column1, 
 column2, 
 ....., 
 column100 
 from Y 
";

您可以使用XML文字来获得类似的行为:

Dim insertSQL As String = <![CDATA[ 
insert into X(
 column1, 
 column2, 
 ....., 
 column100) 
select 
 column1, 
 column2, 
 ....., 
 column100 
 from Y 
]]>.Value

冗长的经典方式是使用:

Dim insertSQL As String = "insert into X( " & vbCrLf &
                          "column1, " & vbCrLf &
                          "column2,  " & vbCrLf &
                          ".....,  " & vbCrLf &
                          "column100)  " & vbCrLf &
                          "select  " & vbCrLf &
                          "column1,  " & vbCrLf &
                          "column2,  " & vbCrLf &
                          ".....,  " & vbCrLf &
                          "column100  " & vbCrLf &
                          "from Y  "