0

我有一个 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

我试图至少导入前两个字段,它们是相应列下的日期和时间,但它似乎不起作用

谁能帮我将此日志解析到数据库中

4

1 回答 1

1

我想在每个循环中清除数据网格行并不是你真正想要的。你需要移动

 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
于 2013-10-13T20:47:12.777 回答