我有一个txt格式的日志文件我想将它导入访问数据库,我作为测试做的第一件事是将它导入数据网格以检查我使用的代码是否可能是下面:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using Reader As New Microsoft.VisualBasic.FileIO.
TextFieldParser(TextBox1.Text)
Reader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
Reader.SetFieldWidths(8, 8)
Dim currentRow As String()
While Not Reader.EndOfData
Try
dg1.Rows.Clear()
currentRow = Reader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
dg1.Rows.Add(currentField)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End Sub
日志文件如下:
6/17/13 9:39AM 103 01 < DISA incoming >71857359 00:01'13" .... 0
6/17/13 9:37AM 102 04 < DISA incoming >71470674 00:03'18" .... 0
6/17/13 9:42AM 102 02 < DISA incoming >71759940 00:00'29" .... 0
6/17/13 9:41AM 103 03 < DISA incoming >71470674 00:01'59" .... 0
6/17/13 9:42AM 102 04 < DISA incoming >76441362 00:00'24" .... 0
6/17/13 9:43AM 103 02 < DISA incoming >70247389 00:01'35" .... 0
我试图至少导入前两个字段,这两个字段是相应列下的日期和时间,但它似乎不起作用
任何人都可以帮我解析这个日志到数据库plz
答案 0 :(得分:1)
我认为在每个循环中清除datagrid行并不是你想要的。 你需要移动
dg1.Rows.Clear()
在while循环之前。此外,但这很难从发布的代码计算,我认为你的Reader.SetFieldWidths(8, 8)
是错误的。试试
Reader.SetFieldWidths(8, 9, -1)
TextFieldParser.SetFieldWidths
然后在循环中你的需要
' Add a new row
Dim curRowIndex = dg1.Rows.Add()
' Set the first cell of the new row....
dg1.Rows(curRowIndex).Cells(0).Value = currentRow(0)
dg1.Rows(curRowIndex).Cells(1).Value = currentRow(1)
dg1.Rows(curRowIndex).Cells(2).Value = currentRow(2) ' the remainder of the line