How to Sort a Dataset in

Posted on 2009-02-19
Last Modified: 2012-05-06
I have a dataset that I read a CSV file into and I use a reader to get access to the fields. My Question is how can I sort the data so that the reader comes out in a specific order? Thanks, Bart
Public Shared Function ReadCSVintoDataSet(ByVal strFileName As String) As DataSet

        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _

             Path.GetDirectoryName(strFileName) & _

             "\;Extended Properties=""text;HDR=Yes;FMT=Delimited"""

        Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)

        Dim da As OleDb.OleDbDataAdapter

        Dim ds As DataSet


            'Open Data Adapter to Read from Text file

            da = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & Path.GetFileName(strFileName) & "]", objConn)

            ds = New DataSet("CSVFile")

            'Fill dataset using dataadapter

            da.Fill(ds, strFileName)

            Return ds

        Catch ex As Exception

            Return Nothing

            ' !!! oopsy MsgBox(ex.Message)

        End Try

    End Function

Open in new window

Question by:bartteems
    LVL 39

    Accepted Solution

    Doesn't a simple ORDER BY work for you? This would only need to be added to the select statement, like so

    da = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & Path.GetFileName(strFileName) & "] ORDER BY myColumnName", objConn)

    Author Closing Comment

    I will give that a try, and if it works I will hit myself in the head for being silly. ;-) Thanks!

    it works, thanks
    LVL 39

    Expert Comment

    Interesting, because I tried the same and couldn't get it to work, although examples (incomplete) on the internet said that it should work... I must've made some silly mistake somewhere. Glad it works for you!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This article will show, step by step, how to integrate R code into a R Sweave document
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    730 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

    18 Experts available now in Live!

    Get 1:1 Help Now