读书人

转换VB.NET语言解决方法

发布时间: 2011-12-29 22:09:38 作者: rapoo

转换VB.NET语言
Dim sqlString As String = "insert tablename " & vbCrLf
Dim selectString As String = " "

Using sr As System.IO.StreamReader = New System.IO.StreamReader( "textFileName ")
Dim line As String
Do
line = sr.ReadLine
If line Is Nothing Then
Exit Do
End If

Dim fields() As String = line.Split( "| ")
If Not IsNothing(fields) AndAlso fields.Length = 3 AndAlso IsNumeric(fields(0).Trim) Then
If selectString.Trim = " " Then
selectString = String.Format( "select {0}, '{1} ',{2} ", fields(0), fields(1), fields(2))
Else
selectString &= vbCrLf & "union all " & vbCrLf & _
String.Format( "select {0}, '{1} ',{2} ", fields(0), fields(1), fields(2))
End If
End If
Loop Until line Is Nothing

sr.Close()


End Using

If selectString <> " " Then
sqlString &= selectString
'执行Sql语句
End If

请大侠帮忙把这段语言转换成VB.NET

[解决办法]
改了一下vs2005 vb.net
,我觉得更好理解一些吧!
Dim textFileName As String = "c:\DbcFile.txt "
Dim sqlString As String = "insert tablename " & vbCrLf
Dim selectString As String = " "
Try
Dim FileString As String = My.Computer.FileSystem.ReadAllText(textFileName)
Dim FileRowText() As String = Strings.Split(FileString, vbCrLf)
Dim nRow As Integer = 0
For nRow = 0 To FileRowText.GetUpperBound(0)
Dim fields() As String = FileRowText(nRow).Split( "| ")
If Not IsNothing(fields) AndAlso fields.Length = 3 AndAlso IsNumeric(fields(0).Trim) Then
If selectString = " " Then
selectString = String.Format( "select {0}, '{1} ',{2} ", fields(0), fields(1), fields(2))
Else
selectString &= vbCrLf & "union all " & vbCrLf & _
String.Format( "select {0}, '{1} ',{2} ", fields(0), fields(1), fields(2))
End If
End If
Next
If selectString <> " " Then
sqlString &= selectString
'执行Sql语句
End If
Catch ex As Exception
MsgBox( "生成SQL语句错误: " & ex.Message)
End Try

说明:
1.原文textFileName应该是一个文件名变量.


[解决办法]
selectString = String.Format("select {0}, "{1} ",{2}", fields(0), fields(1), fields(2))

string.format 的5种重载中好象没有你的这种参数调用呀!

String.Format (String, Object)

String.Format (String, Object[])

String.Format (IFormatProvider, String, Object[])

String.Format (String, Object, Object)

String.Format (String, Object, Object, Object)

读书人网 >VB Dotnet

热点推荐