AlexPonnath
asked on
VB.Net Data Import
Hi,
i am trying to write some code which reads line by line of a text file (CSV) and trys to import them. Here is my 2 issues which i am trying to deal with.
Some fields have a ' in it like Names Walther's etc how do you gracefully handle ths ?
Also some fields have a comma in the field but then the field is enclosed by " how can i check for that ? If i just set my Spliter to , it will see the , in the field as another seperator.
i am trying to write some code which reads line by line of a text file (CSV) and trys to import them. Here is my 2 issues which i am trying to deal with.
Some fields have a ' in it like Names Walther's etc how do you gracefully handle ths ?
Also some fields have a comma in the field but then the field is enclosed by " how can i check for that ? If i just set my Spliter to , it will see the , in the field as another seperator.
ASKER
I dont want to use ADO.net since this function might be called for some data which will not talk to db. So thats not a solution
ASKER
First of i am capable of using google myself so i have seen both of these before... i am using the streamreader already
Dim fr As StreamReader = Nothing
Dim FileString As String = ""
Dim LineItemsArr() As String
Dim FilePath As String = Trim(Me.txtSelectedFile.Te xt)
fr = New System.IO.StreamReader(Fil ePath)
While fr.Peek <> -1
FileString = fr.ReadLine.Trim
If String.IsNullOrEmpty(FileS tring) Then Continue While 'Empty Line
LineItemsArr = FileString.Split(",")
End While
So what i am looking for is code / reference to code which will handle the folowing Line
Mc Donald's, 123, "MC Donald's, LLC"
Dim fr As StreamReader = Nothing
Dim FileString As String = ""
Dim LineItemsArr() As String
Dim FilePath As String = Trim(Me.txtSelectedFile.Te
fr = New System.IO.StreamReader(Fil
While fr.Peek <> -1
FileString = fr.ReadLine.Trim
If String.IsNullOrEmpty(FileS
LineItemsArr = FileString.Split(",")
End While
So what i am looking for is code / reference to code which will handle the folowing Line
Mc Donald's, 123, "MC Donald's, LLC"
I agree: I think the TextFieldParser is a much better idea for this task. I posted a comment showing usage here:
https://www.experts-exchange.com/questions/26899233/Textfieldparser-from-CSV-file-need-to-put-into-a-datatable-or-db-and-show-user.html?anchorAnswerId=35176629#a35176629
Be sure to set the HasFieldsEnclosedInQuotes property to True!
https://www.experts-exchange.com/questions/26899233/Textfieldparser-from-CSV-file-need-to-put-into-a-datatable-or-db-and-show-user.html?anchorAnswerId=35176629#a35176629
Be sure to set the HasFieldsEnclosedInQuotes property to True!
the link i posted is not using streamreader, rather using TextFieldParser class.
read my comment again.
read my comment again.
ASKER
guys i am trying to find a solution for my problem, and the solution should not mean to get rid of streamreader. Based on the links i can not see how i can use streamreader andthe TextFieldParser class together
You said:
e.g
Some fields have a ' in it like Names Walther's etc how do you gracefully handle ths ?Then what we have confirmed is about as graceful as you can get. Yes, you can achieve your goal by continuing on with the StreamReader and writing some logic to account for the commas and quotations, but what you are effectively doing is reinventing a parser. Why do that? From the code you posted, I cannot see what benefit you hope to gain by sticking with a StreamReader. If there is some specific reason why you need a StreamReader, then perhaps you would share that reason with us.
Based on the links i can not see how i can use streamreader andthe TextFieldParser class togetherWell, there is an overload for the constructor which takes in a TextReader object (from which StreamReader derives).
e.g
Dim tfp As New TextFieldParser(fr)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
check this article for a sample, read the section "Using the Jet Engine"
http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom