質問

I've a log file in txt format I want to import it into an access database, the first thing I've done as a test is to import it into a datagrid to check whether that possible the code I used is the below :

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

The Log file is the follwoing :

 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

I was attempting to at least import the first two fields which are the date and time each in under the corresponding column, but it doesn't seem to work

Can anyone help me to parse this log into a database plz

役に立ちましたか?

解決

I suppose that clearing the datagrid rows at every loop is not really what you want. You need to move the

 dg1.Rows.Clear()

before the while loop. Also, but this is difficult to calculate from the code posted, I think that your Reader.SetFieldWidths(8, 8) is wrong. Try with

Reader.SetFieldWidths(8, 9, -1)

TextFieldParser.SetFieldWidths

Then in the loop your need

' 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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top