Before you begin, you have to realize that Microsoft in it's less than infinite wizdom regocnizes that any structured storage system is a potential data source. Bearing that in mind, realize that a csv file is a structured storage source, and because it is, we can open it with ADO.
I recoded your routine to make use of a few ADO objects and now it works like this:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DataFile As FileInfo = New FileInfo("C:\Temp\badger.csv")
Dim cnCSV As OdbcConnection
Dim daCSV As OdbcDataAdapter
Dim dt As DataTable = New DataTable("badger")
cnCSV = New OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & DataFile.Directory.FullName & ";")
daCSV = New OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", Conn)
daCSV.Fill(dt)
DataGrid1.DataSource = dt
End Sub
Whadayamean it doesn't work....
It works fine on my machine!
If you carefully review my code you will see that every object you need to open the data and connect it to your grid is coded. You only need to change the file location for where your file is located and then it will work. Would you like me to send you an example project?
Whadayamean it doesn't work....
It works fine on my machine!
I had a quick question cyberhawke, would it be possible to incorporat a WHERE statement into the "OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", cnCSV)" line? I am trying to make a program that will select from live data and I want to be able to give the program a reference to search for. Would that be possible using this method at all? If not how else could I do it. Thanks a bunch for your help!!!!
P.S. I have not written VB in years and I have very limited knowledge sorry.
This thread is quite old, so I dont know if CyberHawke will see it.. you should be able to just add a Where clause after the ] tho, as that is the end of the SQL statement.
An easier route would be to open the dataset as shown, and then build a set of datarows from the table in the opened dataset using the DataTable.Select method.
Whadayamean it doesn't work....
It works fine on my machine!
File parsers are great if you want to look at the text in a file, but not very useful if you actually want to use the data in the CSV as data in a table driven system. Depends on the need of your system.
Whadayamean it doesn't work....
It works fine on my machine!
Hello CiberHawke
your code runs great, with a few modifications to run it in VB 2005 express, it's very good. The only thing is that I don't get the parsed fields in the datagrid, everything gets in the first (and only) column. Was this code supposed to do the parsing, or do I have to add some code? I'm kind of lost here.