473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 1749
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aikwee | last post by:
how, how do i clear the current content of a datagrid? I am using datagrid to do a xml editor, so every time i try to open anther file and load it to datagrid, it actually append it in to...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
11
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big...
0
by: Sharath | last post by:
"Inspired" by the huge success of our first two automation fast track batches We are forced to start third fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first three automation fast track batches We are forced to start fourth fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first four automation fast track batches We are forced to start fifth fast track automation batch ...
4
by: Ty | last post by:
Hi all Short version of my problem: i have a Datagrid (Flexgrid from ComponentOne) with a Datatable as source. I need to search a row in the datatable, using a primary key column in the...
5
by: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post by:
I have a VS 2008 ASP.NET webform that has a reportview tag on it, accessing an .RLDC report in local report. The columns for the report are essentially: Month Item #1 Item#2 Item#3 ...
3
by: madankarmukta | last post by:
Hi, Objectives is to write the DataTable content to the text file in the format Col1 | Col2 | Col3 ------|-------|--------- Val11 | Val21 | Val31 Val12 | Val22 | Val32 Val13 | Val23 |...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.