Knowing how to load a comma-separated file (CSV) into a dataset using Microsoft Visual Basic.Net (VB.Net) can save you time when working with CSV files. A CSV file is commonly used to import data from one application to another. It can also be compiled by using any spreadsheet application, such as Microsoft Excel. A dataset is used in computer programming to collect data in a tabular form.
- Skill level:
Other People Are Reading
Start Microsoft Visual Basic Express and click "New Project..." on the left pane of your screen. Double-click "Console Application."
Press "Ctrl" and "A," and then press "Delete" to remove existing code.
Copy and paste the following code to your "Module1.vb" module. The code will make the file connection and query the CSV file through a select query. The query results will then be loaded to a dataset, and the number of columns and rows will be displayed through the "Console Window."
Module Module 1
Sub Main() Dim oleString As String oleString = "Provider=Microsoft.Jet.OLEDB.4.0; " oleString = oleString & "Data Source=C:\;Extended Properties=""Text;" oleString = oleString & "HDR=No;FMT=Delimited""" Dim con As New OleDbConnection(oleString) Dim da As New OleDbDataAdapter() Dim ds As New DataSet() Dim oldeComm As New OleDbCommand("SELECT * FROM C:\CSVFile.csv", con) conn.Open() da.SelectCommand = oldeComm ds.Clear() da.Fill(ds, "CSV") Console.Write("Number of columns: " & ds.Tables(0).Columns.Count) Console.Write("Number of rows: " & ds.Tables(0).Rows.Count) conn.Close() End Sub
Edit the following line of code and type the file path to your CSV file:
Dim oldeComm As New OleDbCommand("SELECT * FROM C:\CSVFile.csv", con)
Press "F5" to run your program.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for