将 SQL 命令从 vb .net 转换为 C#

Converting SQL command from vb .net to C#

我正在尝试分析 vb.net 中的 SQL 命令文本并将其集成以与 C# 兼容。

我在将 SQL 命令文本从 vb.net 转换为 C# 时遇到问题。

我使用了 Telerik:http://converter.telerik.com/

并得到了这些结果(如下所示)。我想知道是否有人熟悉 VB.NET 和 C# 可以帮助我重新编写完整的 SQL 命令文本。

原始vb.net命令文本:

Dim r As SqlDataReader = sqlCommand.ExecuteReader()

Do While True
    If r.Read() Then
         SQL2 = "SELECT * from Invoices "
         SQL2 = SQL2 & " WHERE Num = '" & r("Num") & "'"

         conPubs2 = New SqlConnection(<ConnectionString>)
         sqlCommand2 = New SqlCommand(SQL2, conPubs2)
         conPubs2.Open()

         Dim r2 As SqlDataReader = sqlCommand2.ExecuteReader()

         Do While True
             If r2.Read() Then
                 MsgBox(RTrim(r("Num")) & ": " & RTrim(r2("ItemID")))
             Else
                 Exit Do
             End If
         Loop

    Else
        Exit Do
    End If
Loop

conPubs.Close()
conPubs2.Close()

End Sub

这是我在使用 Telerik 的在线转换器时在 C# 中得到的结果:

SqlDataReader r = sqlCommand.ExecuteReader();

while (true)
{
    if (r.Read())
    {
        SQL2 = "SELECT * from Invoices ";
        SQL2 = SQL2 + " WHERE Num = '" + r("Num") + "'";

/* Cannot convert AssignmentStatementSyntax, CONVERSION ERROR: Conversion for XmlElement not implemented, please report this issue in ')
sql...' at character 314

at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.DefaultVisit(SyntaxNode node)
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.VisitXmlElement(XmlElementSyntax node)<br> at Microsoft.CodeAnalysis.VisualBasic.Syntax.XmlElementSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor)
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node)<br> at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node)<br> at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.VisitXmlElement(XmlElementSyntax node)
at Microsoft.CodeAnalysis.VisualBasic.Syntax.XmlElementSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor)<br> at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitSimpleArgument(SimpleArgumentSyntax node)<br> at Microsoft.CodeAnalysis.VisualBasic.Syntax.SimpleArgumentSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor)
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node)<br> at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.VisitSimpleArgument(SimpleArgumentSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.SimpleArgumentSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor) at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.<>c__DisplayClass83_0.<ConvertArguments>b__0(ArgumentSyntax a, Int32 i) at System.Linq.Enumerable.<SelectIterator>d__52.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator1.MoveNext() at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SeparatedList[TNode](IEnumerable1 nodes) at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitArgumentList(ArgumentListSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.ArgumentListSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.VisitArgumentList(ArgumentListSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.ArgumentListSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor) at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.ObjectCreationExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.ObjectCreationExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor) at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.MethodBodyVisitor.VisitAssignmentStatement(AssignmentStatementSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.AssignmentStatementSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node) at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)

我最终会尝试扩展具有以下命令文本的不同查询;我需要对其进行扩展的原因是能够计算与 Num 关联的总行数(返回的行数,例如,如果超过 1 个):

dbConnection.Open();
var sqlCmd = dbConnection.CreateCommand();
sqlCmd.CommandText = @"SELECT [ItemID], [Num]
                       FROM [Test].[dbo].[Invoices]
                       WHERE Num = '" + orderNumber.ToString() + "'";

好的关于转换的第一部分,首先,你没有复制VB.Net的整个代码,它缺少很多部分为了正确转换 为 C# 代码,我做了一些实现并在您使用的同一网站上进行了转换。 VB.Net[理所应当],不代表写对了,不代表不会出错。

Sub Hello()
        Dim conPubs As SqlClient.SqlConnection = New SqlClient.SqlConnection With {.ConnectionString = "Your_ConnectionString"}
        conPubs.Open()
        Dim ThisCommand1 As New SqlClient.SqlCommand 'This line was missing from the example you copied this from.
        Dim r As SqlClient.SqlDataReader = ThisCommand1.ExecuteReader
        Do While True
            If r.Read() Then
                Dim SQL2 As String = "SELECT * from Invoices WHERE Num = '" & r("Num") & "'"
                Dim conPubs2 = New SqlClient.SqlConnection("")
                Dim sqlCommand2 = New SqlClient.SqlCommand(SQL2, conPubs2)
                conPubs2.Open()
                Dim r2 As SqlClient.SqlDataReader = sqlCommand2.ExecuteReader()
                Do While True
                    If r2.Read() Then
                        MsgBox(RTrim(r("Num")) & ": " & RTrim(r2("ItemID")))
                    Else
                        Exit Do
                    End If
                Loop
                conPubs2.Close()       'Is a second SqlServer Connection
            Else
                Exit Do
            End If
        Loop
        conPubs.Close()      'Is SqlServer Connection
    End Sub

向 C# 的转换:

public void Hello()
{
    SqlClient.SqlConnection conPubs = new SqlClient.SqlConnection() { ConnectionString = "Your_ConnectionString" };
    conPubs.Open();
    SqlClient.SqlCommand ThisCommand1 = new SqlClient.SqlCommand(); // This line was missing from the example you copied this from.
    SqlClient.SqlDataReader r = ThisCommand1.ExecuteReader;
    while (true)
    {
        if (r.Read())
        {
            string SQL2 = "SELECT * from Invoices WHERE Num = '" + r("Num") + "'";
            var conPubs2 = new SqlClient.SqlConnection("");
            var sqlCommand2 = new SqlClient.SqlCommand(SQL2, conPubs2);
            conPubs2.Open();
            SqlClient.SqlDataReader r2 = sqlCommand2.ExecuteReader();
            while (true)
            {
                if (r2.Read())
                    MsgBox(RTrim(r("Num")) + ": " + RTrim(r2("ItemID")));
                else
                    break;
            }
            conPubs2.Close();       // Is a second SqlServer Connection
        }
        else
            break;
    }
    conPubs.Close();      // Is SqlServer Connection
}

再次注意:

This answers to your conversion issues, but doesn't mean that VB .Net code is not going to generate Errors.

在 VB.Net 中:

  • VB.Net代码需要重写

  • 考虑对数据库连接使用 [Using ... End Using] 方法。