How can I import a CSV file into a DataTable

Platform: WinForms| Category: Data Binding

Here is a solution suggested by Elan Zhou (MS) on the microsoft.public.dotnet.languages.vb newsgroup.

You can also use the following code:
1. Put a DataGrid on the form.
2. Try the following sample code: (Suppose the csv is c:\test.csv.)


Imports System.Data
Imports System.Data.OleDb


Public Class Form1
	Inherits System.Windows.Forms.Form
	Dim objDataset1 As DataSet()

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
		Dim sConnectionString As String = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=Text;'
		Dim objConn As New OleDbConnection(sConnectionString)
		objConn.Open()

		Dim objCmdSelect As New OleDbCommand('SELECT * FROM test.csv', objConn)
		Dim objAdapter1 As New OleDbDataAdapter()
		objAdapter1.SelectCommand = objCmdSelect

		Dim objDataset1 As New DataSet()
		objAdapter1.Fill(objDataset1, 'Test')
		DataGrid1.DataSource = objDataset1.Tables(0).DefaultView
		objConn.Close()
	End Sub
End Class

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.