I suggest you to use OleDb drivers to read comma separated files.
Steps to do:
1) create new windows application project
2) add controls:
- TextBox (Name: TxtFileToLoad) - here you'll define path to csv file
- DataGridView (Name: DataGridView1) - to display data
- Button (Name: BtnLoadData)
3) copy and paste below code:
Private Sub BtnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoadData.Click
Dim scon As String = String.Empty, scom As String = String.Empty
Dim con As OleDb.OleDbConnection = Nothing, com As OleDb.OleDbCommand = Nothing, rdr As OleDb.OleDbDataReader = Nothing
Dim sFileName As String = String.Empty
Dim dt As Data.DataTable = Nothing, r As DataRow = Nothing
Try
sFileName = Me.TxtFileToLoad.Text
scon = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & IO.Path.GetDirectoryName(sFileName) & ";Extended Properties='Text;HDR=Yes;Schema=schema.ini;';"
scom = CreateCommand(sFileName)
con = New OleDb.OleDbConnection(scon)
con.Open()
com = New OleDb.OleDbCommand(scom, con)
rdr = com.ExecuteReader()
dt = New Data.DataTable
dt.Load(rdr)
Me.DataGridView1.DataSource = dt
Catch ex As OleDb.OleDbException
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
Finally
If Not rdr Is Nothing Then rdr.Close()
If Not com Is Nothing Then com.Dispose()
If Not com Is Nothing Then con.Close() : con.Dispose()
End Try
End Sub
Function CreateCommand(ByVal sFileName As String) As String
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
sb.AppendLine("SELECT *")
sb.AppendLine("FROM " & sFileName & ";")
Return sb.ToString
End Function
4) define
schema.ini
file; store it in the same folder where the data file is placed.
More about schema.ini file (Text File Driver):
http://msdn.microsoft.com/en-us/library/windows/desktop/ms709353%28v=vs.85%29.aspx[
^]
5) Run project
My example data (someData2.csv):
some data 1,271500000000,another data 1, 500
some data 2,381700000000,another data 2, 900
some data 3,211500000000,another data 3, 1500
some data 4,932500000000,another data 4, 300
My example schema:
[someData2.csv]
Format=Delimited(,)
ColNameHeader=FALSE
DecimalSymbol=.
NumberDigits=0
MaxScanRows=0
StartScanRow=0
Col1=Column1 Text Width 30
Col2=Column2 Double
Col3=Column3 Text Width 30
Col4=Column4 Long
DataGridView displays
271500000000
as
271500000000
not
2.72E+11