Geeks With Blogs
Bill Jones Jr. MVP Visual Basic Charlotte NC - MCP C# and VB.Net - Founder and President of the Enterprise Developers Guild (.Net User Group)

Are you still using Framework 1 or 1.1?  Are you using DNN 3?  Did you know there is a function to convert a data reader object into a dataset built into DNN 3?  Neither did I, so I wrote one.  And then I found one Dan Thayer of Logical Advantage had sent me in an email a few months ago.  Dan uses reflection, so his is pretty slick.  If you want to see Dan's solution, let me know.  I'm sure he would be happy to lend his permission for me to publish it.

 

Anyway, it was right after I discovered the second preexisting solution to the same issue that I decided to install the VB Snippet Editor (http://msdn.microsoft.com/vbasic/downloads/tools/default.aspx) so maybe I can reduce the number of times I re-invent the wheel.

 

If you are lucky enough to already be doing production work in Framework 2, all this should be unnecessary.  I’ve seen a load function in the dataset (or data table?) in 2.0 that uses the data reader as input.

 

When we implemented this function, we coded a couple of overloads making the name parameters optional.  Yes, I know we can use the optional attribute directly on the parameters, but that doesn’t play well with some of the less sophisticated CLR languages.  Look it over.  Add it to your Snippets if you like it.  Oh and yes, the VB Snippet Editor requires Framework 2 to run, but it doesn’t mind if you save J#, C# or Framework 1.x code.  The Snippet Editor is definitely a handy tool.

 

     Bill J

        ''' -----------------------------------------------------------------------------
        ''' 
        ''' Uses the dataRdr schema to create a DataTable.  Populates the new DataTable
        ''' from the dataRdr and loads it to a DataSet using the tableName provided.
        ''' 
        Public Shared Function ReaderToDataSet(ByVal dataRdr As IDataReader, _
                                             ByVal tableName As String, _
                                             ByVal datasetName As String) As DataSet
            Dim myTable As New DataTable
            Dim myDS As New DataSet
            Dim myRow As DataRow

            Try
                SetTableSchemaHelper(myTable, dataRdr)
                myTable.TableName = tableName
                myDS.Tables.Add(myTable)

                Do While dataRdr.Read
                    myRow = myTable.NewRow()
                    For i As Integer = 0 To dataRdr.FieldCount - 1
                        myRow(i) = dataRdr.GetValue(i)
                    Next
                    myTable.Rows.Add(myRow)
                Loop

                dataRdr.Close()
                Return myDS

            Catch ex As Exception
                Throw New ApplicationException("ReaderToDataSet - " & ex.Message)
            End Try

        End Function

        ''' -----------------------------------------------------------------------------
        ''' 
        ''' Helper method to add columns to a table from a schema table loaded from
        ''' a DataReader object.  Note the target table is accessed by reference.
        ''' 
        ''' -----------------------------------------------------------------------------
        Public Shared Sub SetTableSchemaHelper(ByRef dtTable As DataTable, _
                                             ByVal dataRdr As IDataReader)
            Dim mySchema As DataTable
            Dim myCol As DataColumn

            mySchema = dataRdr.GetSchemaTable

            For i As Integer = 0 To mySchema.Rows.Count - 1
                myCol = New DataColumn
                With mySchema.Rows(i)
                    myCol.ColumnName = DirectCast(.Item("ColumnName"), String)
                    myCol.AllowDBNull = DirectCast(.Item("AllowDBNull"), Boolean)
                    myCol.AutoIncrement = DirectCast(.Item("IsIdentity"), Boolean)
                    myCol.Unique = myCol.AutoIncrement
                    myCol.DataType = DirectCast(.Item("DataType"), Type)
                    If myCol.DataType Is System.Type.GetType("String") Then
                        myCol.MaxLength = DirectCast(.Item("ColumnSize"), Integer)
                    End If
                    myCol.ReadOnly = DirectCast(.Item("IsReadOnly"), Boolean)
                    If myCol.Unique = False Then
                        myCol.Unique = (myCol.DataType Is System.Type.GetType("GUID"))
                    End If
                End With
                dtTable.Columns.Add(myCol)
            Next i

        End Sub

Posted on Sunday, March 19, 2006 6:16 PM 2. Code Practice | Back to top


Comments on this post: From IDataReader to DataSet

# re: From IDataReader to DataSet
Requesting Gravatar...
Brilliant! Thanks! Creating a custom module for DNN3, and this is exactly what I needed :) Flawless
Left by Christian Knudsen on Jan 07, 2008 7:18 AM

# re: From IDataReader to DataSet
Requesting Gravatar...
what about this ?

DataSet dataSet = new DataSet()

dataSet.Load(reader, ...


Left by Ice on Feb 19, 2008 9:56 AM

# re: From IDataReader to DataSet
Requesting Gravatar...
Hello, I found a lot of useful information, interesting!
Share with my friends!
192.168.1.1
Left by ted on Mar 24, 2016 8:20 AM

# re: From IDataReader to DataSet
Requesting Gravatar...
naucz sie kilka slowek dziennie. wystarczy 5 dziennie no i oczywiscie codziennie powtarzaj.
192.168.l.l
Left by atternse on Jul 28, 2016 3:51 AM

# re: From IDataReader to DataSet
Requesting Gravatar...
Thank you for post :)
192.168.1.1
Left by Link Ci on Dec 25, 2016 3:55 PM

# re: From IDataReader to DataSet
Requesting Gravatar...
If we can only encounter each other rather than stay with each other,then I wish we had never encountered.
192.168.1.1
Left by Daphne on Dec 28, 2016 7:04 PM

# re: From IDataReader to DataSet
Requesting Gravatar...
I would like weeping with the smile rather than repenting with the cry,when my heart is broken ,is it needed to fix?
192.168.1.1
Left by Daphne on Dec 28, 2016 7:06 PM

# re: From IDataReader to DataSet
Requesting Gravatar...
Everything ought to be beautiful in a human being: face, dress, soul and idea.
192.168.1.1
Left by Fawziya on Dec 28, 2016 7:08 PM

# re: From IDataReader to DataSet
Requesting Gravatar...
Remember what should be remembered, and forget what should be forgotten.Alter what is changeable, and accept what is unchangeable.
192.168.1.1
Left by Halona on Dec 28, 2016 7:09 PM

# re: From IDataReader to DataSet
Requesting Gravatar...
tanks for the valueable information
direct TV sur internet
Left by adam on Jan 03, 2017 12:49 PM

# re: From IDataReader to DataSet
Requesting Gravatar...
The default gateway IP address for router is
192.168.1.1
Left by Lucylu on Apr 10, 2017 10:01 AM

# re: From IDataReader to DataSet
Requesting Gravatar...
A default router Ip address is very much needed to access the
settings
192.168.0.1
Left by Richard on Apr 11, 2017 7:47 AM

# re: From IDataReader to DataSet
Requesting Gravatar...
A valuable input and strong basement
192.168.1.254
Left by Dsoza on Apr 11, 2017 7:53 AM

Your comment:
 (will show your gravatar)


Copyright © Bill Jones Jr. | Powered by: GeeksWithBlogs.net