Vb.net 从日志文件解析

Vb.net 从日志文件解析,vb.net,parsing,logfile,Vb.net,Parsing,Logfile,我有一个txt格式的日志文件,我想将其导入access数据库,作为测试,我做的第一件事是将其导入datagrid,以检查我使用的代码是否可能如下所示: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Using Reader As New Microsoft.VisualBasic.FileIO. TextFieldParser(TextBox1.Text)

我有一个txt格式的日志文件,我想将其导入access数据库,作为测试,我做的第一件事是将其导入datagrid,以检查我使用的代码是否可能如下所示:

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
6/17/13 9:39AM 103 0171857359 00:01'13”…0
2013年6月17日上午9:37 102 0471470674 00:03'18“。。。。0
2013年6月17日上午9:42 102 0271759940 00:00'29“…0
2013年6月17日上午9:41 103 0371470674 00:01'59“。。。。0
2013年6月17日上午9:42 102 0476441362 00:00'24“…0
2013年6月17日上午9:43 103 0270247389 00:01'35“。。。。0
我试图至少导入前两个字段,分别是对应列下的日期和时间,但似乎不起作用


有人能帮我把这个日志解析成数据库吗?我想在每个循环中清除datagrid行并不是你真正想要的。 你需要把车开走

 dg1.Rows.Clear()
在while循环之前。还有,但是这很难从发布的代码中计算出来,我认为您的
Reader.SetFieldWidths(8,8)
是错误的。试一试

Reader.SetFieldWidths(8, 9, -1)

然后把你的需要放在循环中

' 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

你能发布几行输入文件吗?最后的东西只是多余的吗?前两个的具体问题是什么?它们应该是一个单一的日期时间吗?由于我们看不到原始数据,也不知道数据库模式,我们一直在猜测。我想,在每个循环中清除datagrid行并不是您真正想要的,请移动这些行。清除while loopOk之外的数据。问题出在dg1.rows.Add(currentField)它只取第一部分,即日期,在第二行中它添加时间,第三行是日志第二行的第二个日期,但我需要添加的行与日志完全相同,因此它应该先取日期,然后取时间,并将它们添加到同一行Steve,我的问题是dg1.rows.add(currentfield)我需要像currentfield(0)一样是日期部分,currendfield(1)是时间的第二列,所以它看起来像dg1.rows.add(currentfield(0),currentfield(1)等),我怎么做呢?我想他正在尝试将
dg1.rows(curRowIndex.Cells(0).Value
作为由
currentwrow(0)组成的新的日期时间值
当前行(1)
(??)史蒂夫,你救了我一晚,非常感谢,伙计,这就是我要找的:)