从vb.net到c的sql命令转换#

anauzrmj  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(389)

我正在尝试分析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") + "'";

/*无法转换assignmentstatementsyntax,转换错误:未实现xmlelement的转换,请在“”中报告此问题)
sql…'字符314
在icharpCode.codeconverter.csharp.visualbasicconverter.nodesvisitor.defaultvisit(syntaxnode节点)
在microsoft.codeanalysis.visualbasic.visualbasicsyntaxvisitor 1.VisitXmlElement(XmlElementSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.XmlElementSyntax.Accept[TResult](VisualBasicSyntaxVisitor 1名访客)
在microsoft.codeanalysis.visualbasic.visualbasicsyntaxvisitor 1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor 1.visitxmlement(xmlementsyntax节点)
在microsoft.codeanalysis.visualbasic.syntax.xmlelementsyntax.accept[tresult](visualbasicsyntaxvisitor 1 visitor) at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitSimpleArgument(SimpleArgumentSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.SimpleArgumentSyntax.Accept[TResult](VisualBasicSyntaxVisitor 1名访客)
在microsoft.codeanalysis.visualbasic.visualbasicsyntaxvisitor 1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor 1.访问microsoft.codeanalysis.visualbasic.syntax.simpleargumentsyntax.accept[tresult](visualbasicsyntaxvisitor)上的SimpleArgument(simpleargumentsyntax节点) 1 visitor) at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.<>c__DisplayClass83_0.<ConvertArguments>b__0(ArgumentSyntax a, Int32 i) at System.Linq.Enumerable.<SelectIterator>d__5 2.system.linq.enumerable.whereenumerableiterator处的movenext() 1.MoveNext() at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SeparatedList[TNode](IEnumerable 1个节点)位于microsoft.codeanalysis.visualbasic.syntax.argumentlistsyntax.accept[tresult](visualbasicsyntaxvisitor 1 visitor) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor 1.访问icsharpcode.codeconverter.csharp.commentconvertingnodesvisitor.defaultvisit(syntaxnode节点),访问microsoft.codeanalysis.visualbasic.visualbasicsyntaxvisitor 1.VisitArgumentList(ArgumentListSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.ArgumentListSyntax.Accept[TResult](VisualBasicSyntaxVisitor 1个访问者)位于microsoft.codeanalysis.visualbasic.syntax.objectcreationexpressionsyntax.accept[tresult](visualbasicsyntaxvisitor)的icsharpcode.codeconverter.csharp.visualbasicconverter.nodesvisitor.VisutObjectCreationExpression(objectcreationexpressionsyntax节点) 1 visitor) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor 1.访问icsharpcode.codeconverter.csharp.commentconvertingnodesvisitor.defaultvisit(syntaxnode节点),访问microsoft.codeanalysis.visualbasic.visualbasicsyntaxvisitor 1.VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.ObjectCreationExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor 1个访问者)位于microsoft.codeanalysis.visualbasic.syntax.assignmentstatementsyntax.accept[tresult](visualbasicsyntaxvisitor 1 visitor) at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor 1.访问icsharpcode.codeconverter.csharp.commentconvertingmethodbodyvisitor.convertwithtrivia(syntaxnode节点)icsharpcode.codeconverter.csharp.commentconvertingmethodbodyvisitor.DefaultVisition(syntaxnode节点)
最后,我尝试扩展一个不同的查询,其中包含以下命令文本;我需要扩展它的原因是能够计算与num相关联的行的总数(返回的行,例如,如果多于1):

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

ctrmrzij1#

好的,关于转换的第一部分,首先,你没有复制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
}

再次注意:
这解决了转换问题,但并不意味着vb.net代码不会产生错误。
在vb.net中:
vb.net代码需要重新编写。
考虑使用[使用。。。使用]方法结束数据库连接。

相关问题