D
DraguVaso
Hi,
I need a FAST way to put the content of a file in a datatable (one record
for each line in the file).
I have a routine for it, but it takes me too much time (2-5 seconds for each
file) to put the lines in the datatable and do some actions. Does anybody
knows a faster method? The fact is: I actually only need the lines starting
with "0*" and "1*".
What I do now is:
- reading all the lines into a datatable
- I make a dataview from the datatable and apply a filter so I have only the
"0*" and "1*"-records left.
- I do some actions with the 0- and 1-records.
To make it faster I can do folowing things:
- only putting the 0-records and 1-records in the datatable (so I don't have
to apply a filter afterwards). But I'd prefer to put everything in it for
eventually future use of other records.
- I guess I lose most time with opening the file and reading every line in
it. Or am I wrong? Is there a way to do it much faster than I do? (see
routine at the end of the message)
Thanks in advance!!
Pieter
My lines-of-file-reading-into-datatable-routine:
Public Function ReadFileToDataTable(ByVal strDir As String, ByVal
strFile As String) As DataTable
Dim intMyFile As Integer = FreeFile()
Dim strLine As String
Dim myTable As DataTable = New DataTable("FileTable")
Dim colID As DataColumn = New DataColumn("ID",
Type.GetType("System.Int32"))
colID.AutoIncrement = True
colID.AutoIncrementSeed = 0
colID.AutoIncrementStep = 1
myTable.Columns.Add(colID)
Dim colText As DataColumn = New DataColumn("Text",
Type.GetType("System.String"))
myTable.Columns.Add(colText)
Dim NewRow As DataRow
Try
FileOpen(intMyFile, MakePathFile(strDir, strFile),
OpenMode.Input, OpenAccess.Read, OpenShare.Shared, -1)
'once the file is opened
'loop through every line and copy them to our
'variable
Do While Not EOF(intMyFile)
strLine = LineInput(intMyFile)
NewRow = myTable.NewRow()
NewRow("Text") = strLine
myTable.Rows.Add(NewRow)
Loop
myTable.AcceptChanges()
Catch ex As Exception
MessageBox.Show(ex.Message & ex.StackTrace, "Exception")
Finally
'sluiten van het bestand
FileClose(intMyFile)
ReadFileToDataTable = myTable
End Try
End Function
I need a FAST way to put the content of a file in a datatable (one record
for each line in the file).
I have a routine for it, but it takes me too much time (2-5 seconds for each
file) to put the lines in the datatable and do some actions. Does anybody
knows a faster method? The fact is: I actually only need the lines starting
with "0*" and "1*".
What I do now is:
- reading all the lines into a datatable
- I make a dataview from the datatable and apply a filter so I have only the
"0*" and "1*"-records left.
- I do some actions with the 0- and 1-records.
To make it faster I can do folowing things:
- only putting the 0-records and 1-records in the datatable (so I don't have
to apply a filter afterwards). But I'd prefer to put everything in it for
eventually future use of other records.
- I guess I lose most time with opening the file and reading every line in
it. Or am I wrong? Is there a way to do it much faster than I do? (see
routine at the end of the message)
Thanks in advance!!
Pieter
My lines-of-file-reading-into-datatable-routine:
Public Function ReadFileToDataTable(ByVal strDir As String, ByVal
strFile As String) As DataTable
Dim intMyFile As Integer = FreeFile()
Dim strLine As String
Dim myTable As DataTable = New DataTable("FileTable")
Dim colID As DataColumn = New DataColumn("ID",
Type.GetType("System.Int32"))
colID.AutoIncrement = True
colID.AutoIncrementSeed = 0
colID.AutoIncrementStep = 1
myTable.Columns.Add(colID)
Dim colText As DataColumn = New DataColumn("Text",
Type.GetType("System.String"))
myTable.Columns.Add(colText)
Dim NewRow As DataRow
Try
FileOpen(intMyFile, MakePathFile(strDir, strFile),
OpenMode.Input, OpenAccess.Read, OpenShare.Shared, -1)
'once the file is opened
'loop through every line and copy them to our
'variable
Do While Not EOF(intMyFile)
strLine = LineInput(intMyFile)
NewRow = myTable.NewRow()
NewRow("Text") = strLine
myTable.Rows.Add(NewRow)
Loop
myTable.AcceptChanges()
Catch ex As Exception
MessageBox.Show(ex.Message & ex.StackTrace, "Exception")
Finally
'sluiten van het bestand
FileClose(intMyFile)
ReadFileToDataTable = myTable
End Try
End Function