Posted by: Red Souldier | July 17, 2009

ASP.NET – Convert CSV to Dataset

Here is the function to convert CSV file to dataset in using
First you need to upload the csv file into your web server, then save the file path.
Add this function code:

    Private Shared Function getCsvToDataset(ByVal strPath As String) As DataSet
        Dim strLine As String
        Dim strArray As String()
        Dim charArray As Char() = New Char() {","c}
        Dim ds As New DataSet()
        Dim dt As DataTable = ds.Tables.Add("TheData")
        Dim aFile As New FileStream(strPath, FileMode.Open)
        Dim sr As New StreamReader(aFile)

        strLine = sr.ReadLine()

        strArray = strLine.Split(charArray)

        For x As Integer = 0 To strArray.GetUpperBound(0)

        strLine = sr.ReadLine()
        While strLine IsNot Nothing
            strArray = strLine.Split(charArray)
            Dim dr As DataRow = dt.NewRow()
            For i As Integer = 0 To strArray.GetUpperBound(0)
                dr(i) = strArray(i).Trim()
            strLine = sr.ReadLine()
        End While
        Return ds
    End Function

I think, this is the most effective way to convert csv to dataset. Before this, I already try to use Microsoft.Jet.OLEDB.4.0 but the problem that shows an error message in finding csv file on the web server. So this method using FileStream is the best way.



  1. Great blog, reading it through RSS feed as well

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: