.CSV import to DB through VB.NET

Posted on 2006-05-22
Last Modified: 2012-06-27
I need to write a program that imports data from .csv file into a database.

This was prev. being done through a dts that I created but the customers find it difficult to use the DTS even the documentation breaks it down very well.

Hello everyone

I am more of a sql/vbs scripter than a .net programmer so please try not to laugh at me

I need to access the .csv file which was .xls just saved as a .csv it has two rows

row1 = workspaces
row2 = extensions

I need to loop through row 1 and grab the workspaces and insert them into 7 tables
r2 will be done the same way but inserted into 3 tables

how do I access the .csv file and  how should I create those loops

please don't just tell the class can you also give me sample sytax on how to use the class

Thank you in advance
Question by:conflikq
    LVL 9

    Expert Comment

    Check my comments to a similar question in....
    LVL 96

    Expert Comment

    by:Bob Learned
    What version of .NET do you have?

    LVL 1

    Author Comment

    I have ver. 2005 pro
    LVL 96

    Accepted Solution

    Cool b-)

    I have been reading about the TextFieldParser.  Here is a class that should get you started on your way:

    Imports Microsoft.VisualBasic.FileIO

    Public Class CsvReader

        Public Sub ReadFile(ByVal fileName As String)

            Using reader As New TextFieldParser(fileName)

                reader.TextFieldType = FieldType.Delimited

                ' Set the delimiters for comma separated text
                reader.Delimiters = New String() {","}

                Dim currentRow As String()

                'Loop through all of the fields in the file.
                'If any lines are corrupt, report an error and continue parsing.
                While Not reader.EndOfData
                        currentRow = reader.ReadFields()
                        ' Include code here to handle the row.
                    Catch ex As MalformedLineException
                        MsgBox("Line " & ex.Message & " is invalid.  Skipping")
                    End Try
                End While
            End Using

        End Sub

    End Class

    LVL 1

    Author Comment

    I'll try that


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now