How to Sort a Dataset in VB.net?

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
 
        Try
            '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

bartteemsAsked:
Who is Participating?
 
abelCommented:
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)
0
 
bartteemsAuthor Commented:
I will give that a try, and if it works I will hit myself in the head for being silly. ;-) Thanks!

it works, thanks
0
 
abelCommented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.