General discussion

Locked

Visual Basic CSV Reader

By Frenchwood ·
I am trying to read a CSV file usng VB.

I have the current code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStream As IO.Stream
Dim openFileDialog1 As New OpenFileDialog()
Dim CurrentField As String
Dim CurrentRow As String()

openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "Comma Seperated files (*.csv)|*.csv" '|All files (*.*)|*.*
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(myStream)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")

While Not MyReader.EndOfData
Try
CurrentRow = MyReader.ReadFields()
For Each CurrentField In CurrentRow
MsgBox("'" & CurrentField & "',") ' This is temporary to test fields
Next
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
End Using
myStream.Close()
End If
End If
End Sub

What i need is to find 3 different columns, and then select values from these 3 columns based on data in a forth. Ideas anyone??

This conversation is currently closed to new comments.

10 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by NZ_Justice In reply to Visual Basic CSV Reader

Try using an array.

Collapse -

by Frenchwood In reply to

Sorry, not entirely sure how to go about that.

Could you post an example?

Cheers

Collapse -

by Frenchwood In reply to Visual Basic CSV Reader

Note that the columns i need are column1 , 2, & 8 based on a date value that appears in column 28 (of 29)

Thanks in Advance

Collapse -

by Frenchwood In reply to Visual Basic CSV Reader

Point value changed by question poster.

Collapse -

by NZ_Justice In reply to Visual Basic CSV Reader

Have a look here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconusingarrays.asp

Collapse -

by Frenchwood In reply to

A multi-dimensional array would not be big enough to take all the data, as there are over 5 thousand rows. any other ideas?

Cheers

Collapse -

by Tony Hopkinson In reply to Visual Basic CSV Reader

You could use an array but it would be PIA.
Define a class corresponding to the record (or even just the fields you want). Then a collection of them. That will cope with the variable number of records nicely, and easy to enhance.
The For each isn't really helping you out either with this idea and you'd be better off identifying them by index.
The class wizard in the IDE will whop up most of this for you.
You can even stick a LoadFromFile method on the collection and keep the whole thing nicely encapsulated. In fact you can do a SaveToFile as well.

There are some potential generic solutions as well, though unless you've some use for it, probably not worth the effort.

The generic idea would require unique column names ,preferably in the file. Then define a collection, add a field value to it with id of columnname.
Then have a class that only holds this collection which becomes your record.
Then define a collection of the 'record' class which becomes the file.
You can access it in all sorts of ways then especially if each record has a unique identifier.

Collapse -

by Frenchwood In reply to

I have managed to do this by uploading each line to oracle as soon as the variables in the sql statement are initialised. Looping through until all lines in the csv are finished. Works well apart from... See top comment.

Collapse -

by Frenchwood In reply to Visual Basic CSV Reader

This question is now closed.

I have a another question on the sae project here:

http://techrepublic.com.com/5208-11186-0.html?forumID=51&threadID=188003

Collapse -

by Frenchwood In reply to Visual Basic CSV Reader

This question was closed by the author

Back to Web Development Forum
10 total posts (Page 1 of 1)  

Related Discussions

Related Forums