Fill Dataset from csv file

  • Thread starter Thread starter andrei
  • Start date Start date
A

andrei

Hi Group,

I need to fill a dataset from a csv file. The issue is that the file is
already loaded in memory
I'm using an asp.net application to upload a csv file from the user, so
after submitting, the file is uploaded in the server's memory.
I don't want to have to save the file on the disk before accessing it.

Is there any way of loading a dataset this way, directly from memory ?

Thank you for any ideas !

Andrei.
 
I know of no way with the built in capabilities of ADO .NET (will have to
think some more on the problem, as there may be a way). You can use stream
objects to get at the data, but you will likely have to create the arrays to
load it into a DataTable and load each row yourself. You could also load the
CSV into XML (DataSet format) and load in. This can be done from a stream
object, if I remember correctly, but you are still cursing through every row
to set this up.

The issue is this. The ADO .NET providers are set up to pull from a
persistent store, whether it is a database server or a file. There is an
overload to deserilize,
DataSet Constructor (SerializationInfo, StreamingContext), but it is an
internal method. You still end up with a lot of building if you use this
(which is not recommended).

If you temporarily save off the file, you can consume using ADO .NET, which
is simpler. This is not what you desire, however.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
¤ Hi Group,
¤
¤ I need to fill a dataset from a csv file. The issue is that the file is
¤ already loaded in memory
¤ I'm using an asp.net application to upload a csv file from the user, so
¤ after submitting, the file is uploaded in the server's memory.
¤ I don't want to have to save the file on the disk before accessing it.
¤
¤ Is there any way of loading a dataset this way, directly from memory ?

How are you uploading the file into memory?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Hi Andrei,

Yes of course, split your lines using splits line by line in an array an set
the items of your dataset in a simple loop.

Something as in pseudo
For each row as string in mytextfile
myitemsarray = row.split(delimiter)
dr as datarow = table.newrow
for i as integer from 0 to myitemarray.length - 1
dr(i) = myitmarray(i)
next
next

Now I see I could have done it completly

I hope this helps?
Cor
 
Thanks for the solution, Cor, but I still have some problems with this :

The file uses commas as field separators.
All the character type fields are also enclosed in double quotes.
Some fields may contain commas - for example an address field could look
like "123, Long St., Anytown, NY".

So, when splitting by comma character, it would create more fields than
necessary...

This solution would work if there were pipe characters as delimiters, for
example, instead of commas.

Any other ideas ?

Thank you.
 
Hi Paul,

I'm using an asp.net application.
This is the code to read the uploaded file into a byte array.


Dim file As HttpPostedFile = txtContactDetails.PostedFile
Dim intFileLen As Integer = file.ContentLength

Dim b(intFileLen) As Byte

file.InputStream.Read(b, 0, intFileLen)

txtContactDetails is a file control on the aspx page like :
<input id="txtContactDetails" type="file" runat="server"...>

Thank you for any ideas.

Andrei.


 
Hi Andrei,

Are you sure that it is this because than you can in advance first change
all comma and blank change in somethign as {} and than afterwards that back
to comma blank.

I had a sample for what is what you have a real CSV file in memory, however
I cannot find it anymore and searching on my name in the newsgroups is also
not the solution.

However when it is that easy that it is alway comma blank than I would do
that even when I had that sample.

:-)

Cor
 
¤ Hi Paul,
¤
¤ I'm using an asp.net application.
¤ This is the code to read the uploaded file into a byte array.
¤
¤
¤ Dim file As HttpPostedFile = txtContactDetails.PostedFile
¤ Dim intFileLen As Integer = file.ContentLength
¤
¤ Dim b(intFileLen) As Byte
¤
¤ file.InputStream.Read(b, 0, intFileLen)
¤
¤ txtContactDetails is a file control on the aspx page like :
¤ <input id="txtContactDetails" type="file" runat="server"...>
¤

Other than writing code to parse the data from the input stream and adding it to a DataSet I'm not
aware of any way to do this directly. You could use ADO.NET, Jet OLEDB and the Test ISAM driver but
you would need to save it to disk first, if only temporarily, to read in the contents of the file
directly into a DataSet.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Back
Top