By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
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 :-)

"Cor" <no*@non.com> wrote in message
news:O0**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.