I am trying to analyze SQL command text in vb.net and integrate it to be compatible with C#.
I am having issues converting SQL command text from vb.net to c#.
I used Telerik: http://converter.telerik.com/
and got these results (shown below). I was wondering if someone who is familiar with both VB.NET and C# could help me re-write the full SQL command text.
Original vb.net command text:
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
Here's what I got in C# when I used Telerik's online converter:
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 314at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.DefaultVisit(SyntaxNode node)
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.VisitXmlElement(XmlElementSyntax node)1 visitor)
at Microsoft.CodeAnalysis.VisualBasic.Syntax.XmlElementSyntax.Accept[TResult](VisualBasicSyntaxVisitor
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node)1.VisitXmlElement(XmlElementSyntax node)
at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node)
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor
at Microsoft.CodeAnalysis.VisualBasic.Syntax.XmlElementSyntax.Accept[TResult](VisualBasicSyntaxVisitor1 visitor)1 visitor)
at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitSimpleArgument(SimpleArgumentSyntax node)
at Microsoft.CodeAnalysis.VisualBasic.Syntax.SimpleArgumentSyntax.Accept[TResult](VisualBasicSyntaxVisitor
at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor1.Visit(SyntaxNode node)1.VisitSimpleArgument(SimpleArgumentSyntax node) at Microsoft.CodeAnalysis.VisualBasic.Syntax.SimpleArgumentSyntax.Accept[TResult](VisualBasicSyntaxVisitor
at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.VisualBasic.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)
I am trying ultimately, to expand on a different query that has the following command text; the reason why is I need to expand upon it is to be able to count the total number of lines associated with the Num (lines returned, e.g. if more than 1):
dbConnection.Open();
var sqlCmd = dbConnection.CreateCommand();
sqlCmd.CommandText = @"SELECT [ItemID], [Num]
FROM [Test].[dbo].[Invoices]
WHERE Num = '" + orderNumber.ToString() + "'";
New SqlConnection(<ConnectionString>)isn't even valid syntax for VB.NET, so fix that (passing "" should be sufficient for this purpose) and retry the conversion. However, this is very bad VB.NET code to begin with, so you're going to get equally bad C# code out of a conversion. You'd be much better off figuring out the end goal of that code and rewriting the C# code from scratch.<ConnectionString>is a placeholder which is replaced, in the code, as some sort of precompile step.