Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strSrc As String = "01234,ABC," + Chr(34) + "AA,BB" + Chr(34) + ",ABC"
Dim strMid As String() = strSrc.Split(Chr(34))
Dim nLoop As Integer = 0
Dim nTotal As Integer = strMid.Length
Dim strTmp As String(), strLast As String()
Dim nLastLen = 0
While (nLoop < nTotal)
ReDim strTmp(0)
If ((nLoop + 1) Mod 2) Then
strTmp = strMid(nLoop).Split(",")
Else
strTmp(0) = strMid(nLoop)
End If
nLastLen = strLast.Length
nLoop = nLoop + 1
End While
nLoop = 0
nTotal = strLast.Length
While (nLoop < nTotal)
If (strLast(nLoop).Length) Then
Debug.WriteLine(strLast(nLoop))
End If
nLoop = nLoop + 1
End While
End Sub
End Class
AAA,BBB,CCC,DDD
01234,ABC,"AA,BB",ABC
其实上面这种以逗号为分隔的文本格式,是一种CSV文件格式来的。
读取CSV格式的代码如下:
不过需要注意的地方就是原文本格式应为:
----------------------------------
[空行]
AAA,BBB,CCC,DDD
01234,ABC,"AA,BB",ABC
----------------------------------
Public Function GetDataSetFromCSV(ByVal filePath As String, ByVal fileName As String) As DataSet
Dim strConn As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="
strConn += filePath 'filePath, For example: C:\
strConn += ";Extensions=asc,csv,tab,txt;"
Dim objConn As OdbcConnection = New OdbcConnection(strConn)
Dim dsCSV As DataSet = New DataSet
Try
Dim strSql As String = "select * from " + fileName 'fileName, For example: 1.csv
Dim odbcCSVDataAdapter As OdbcDataAdapter = New OdbcDataAdapter(strSql, objConn)
odbcCSVDataAdapter.Fill(dsCSV)
Return dsCSV
Catch ex As Exception
Throw ex
End Try
End Function
Dim rxg As New System.Text.RegularExpressions.Regex("((""[\w, ]+"")|([\w ]+))")
Dim ResultCollection As System.Text.RegularExpressions.MatchCollection = _
rxg.Matches("""AA, BB, c"", 01234,ABC,""AA,BB"",ABC ,x ,""A, BB""")
For Each MatchResult As System.Text.RegularExpressions.Match In ResultCollection
Debug.WriteLine(MatchResult.Groups(0).ToString())
Next
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.TextBox1.Text = "" Then MsgBox("请先选择源数据") : Exit Sub
Dim strcon As String = "Data Source=PC1\sqlexpress;Initial Catalog=mydatabase2005;Integrated Security=True"
Dim mycon As New SqlConnection(strcon)
mycon.Open()
Dim mycom As New SqlCommand
Dim strsql As String
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Dim filenum As Integer = FreeFile()
Dim temp, ip, iparea, place As String
FileOpen(filenum, Ofd.FileName, OpenMode.Input)
'开始读取行
Me.Label1.Text = 0
Do While Not EOF(filenum)
temp = LineInput(filenum)
If Trim(temp) = "" Then GoTo jumpline
ip = "'" & temp.Substring(0, 15) & "'"
iparea = "'" & temp.Substring(16, 15) & "'"
place = "'" & temp.Remove(0, 31) & "'"
strsql = "insert into ipinfor ( ip , iparea ,place ) values ( " & ip & " , " & iparea & " , " & place & " )"
mycom.Connection = mycon
mycom.CommandText = strsql
mycom.ExecuteNonQuery()
Loop
FileClose(filenum)
''>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
jumpline: '显示数据
strsql = "select * from ipinfor"
Dim myadapter As New SqlDataAdapter(strsql, mycon)
Dim mydataset As New DataSet
myadapter.Fill(mydataset, "mytable")
Me.DataGridView1.DataSource = mydataset.Tables("mytable")
mycon.Close() REM 关闭连接
End Sub