How to Load a CSV File to a Dataset

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Load a CSV File to a Dataset
Load CSV files to a dataset using VB.Net. (stock image of binary code sphere image by Ruslana Stovner from Fotolia.com)

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:
Moderate

Other People Are Reading

Instructions

  1. 1

    Start Microsoft Visual Basic Express and click "New Project..." on the left pane of your screen. Double-click "Console Application."

  2. 2

    Press "Ctrl" and "A," and then press "Delete" to remove existing code.

  3. 3

    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."

    Imports System.Data.OleDb

    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
    

    End Module

  4. 4

    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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.