Bulk Load text into dataset?

M

Marc Miller

I have a non-delimited fixed-field text file with a record length of 5919
bytes long that I need
to load into an existing empty dataset with 159 columns.

Currently I am building a mapping like this:

Do While sr.Peek() >= 0
dr = dt.NewRow
dt.Rows.Add(dr)
Dim nLine As String = sr.ReadLine()
dt.Rows(iRow).Item("HH") = nLine.Substring(0, 2)
dt.Rows(iRow).Item("MM") = nLine.Substring(2, 2)
dt.Rows(iRow).Item("District") = nLine.Substring(4, 4)
dt.Rows(iRow).Item("Exchange") = nLine.Substring(8, 8)
dt.Rows(iRow).Item("Remote") = nLine.Substring(16, 8)
dt.Rows(iRow).Item("Company") = nLine.Substring(24, 3)
'''' ETC, ETC, ETC for 159 columns!..................................
Loop

Is there some sort of 'bulk load' the data? This seems awfully cumbersome.

Thanks in advance to all,
Marc
 
M

Marvin Varela

You could use a regular expression like:

(?<HH>..)(?<MM>..)(?<District>..)(?<Exchange>..)(?<Remote>..)(?<Company>..)<
ETC, ETC,>

and then use the following code:

Do While sr.Peek() >= 0
dr = dt.NewRow
dt.Rows.Add(dr)
Dim nLine As String = sr.ReadLine()
Regex reColumnsParser = new
Regex("(?<HH>..)(?<MM>..)(?<District>..)(?<Exchange>..)(?<Remote>..)(?<Compa
ny>..)")
Match mLine = reColumnParser.Match(nLine)
If(mLine.Success)
foreach(DataColumn CurrentColumn in dt.Columns)
dt.Rows(iRow).Item(CurrentColumn.Name) =
mLine.Groups.Item(CurrentColumn.Name)
Loop

If you don't know how to use Regex you can let me know and i will give you
further help about it.
 

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