FAST way to put the content of a file in a DataTable

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
 
C

Cor

Hi Pieter,

The only thing I would add is (roughly typed not checked)
Do While Not EOF(intMyFile)
strLine = LineInput(intMyFile)

if strLine.substring(0,2) = "*0" or strLine.substring(0,2) = "*1" then
NewRow = myTable.NewRow()
NewRow("Text") = strLine
myTable.Rows.Add(NewRow) end if
Loop
myTable.AcceptChanges() '???? why are you doing updates later
that you want to keep track on?

I hope this helps a little bit.

Cor
 
D

DraguVaso

Hm, thanks.
If I understand it good I don't need to use the "myTable.AcceptChanges() "?
I thought it was needed to add the rows but I guess I'm wrong? hehe :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top