SqlDataReader to DataTable
Tuesday, March 21, 2006 @ 3:20 pm by -DS-It’s not a good practice to return a SqlDataReader from a function, but, I like to do that because it’s easier to work with DataReaders than DataTables. Why is bad to return Data Readers? Because the object keeps a connection to the database open until the object gets destroyed or you close it manually.
Why I like to work with Data Readers?
Because the code looks more “readable” and the Data Reader acts more like a Recordset from the old
-
Public Function DataReaderToDataTable(ByVal rdrReader As SqlDataReader) As DataTable
-
'This function receives a Data Reader and returns a Data Table
-
-
Try
-
-
'Table Schema
-
Dim schemaTable As DataTable = rdrReader.GetSchemaTable()
-
-
'Data Table
-
Dim dataTable As DataTable = New DataTable
-
Dim intCounter As Integer
-
'Now to create the Schema on the DataTable
-
For intCounter = 0 To schemaTable.Rows.Count - 1
-
'Current Row
-
Dim dataRow As DataRow = schemaTable.Rows(intCounter)
-
'Current Column Name
-
Dim columnName As String = CType(dataRow("ColumnName"), String)
-
'Current Column
-
Dim column As New DataColumn(columnName, _
-
CType(dataRow("DataType"), Type))
-
'Add Column to the DataTable
-
dataTable.Columns.Add(column)
-
Next intCounter
-
'Now to fill the table with the reader
-
While rdrReader.Read()
-
'New Row
-
Dim dataRow As DataRow = dataTable.NewRow()
-
'Loop the fields
-
For intCounter = 0 To rdrReader.FieldCount - 1
-
'Insert the current value of the DataReader to the DataRow
-
dataRow(intCounter) = rdrReader.GetValue(intCounter)
-
Next
-
-
'Insert the Row into the DataTable
-
dataTable.Rows.Add(dataRow)
-
End While
-
-
'Close the reader
-
rdrReader.Close()
-
'Return the DataTable
-
Return dataTable
-
-
Catch ex As Exception
-
MsgBox(ex.Message)
-
End Try
-
End Function
-
-
Public Function DataTableToDataView(ByVal DTable As DataTable) As DataView
-
'This only works for a SINGLE table
-
Dim DV As DataView
-
DV = DTable.DefaultView
-
Return DV
-
End Function
Then I create another function that returns the information I want, it goes something like this:
-
Public Function GetNewSignatures() As DataView
-
-
Dim DV As DataView
-
V = DataTableToDataView(DataReaderToDataTable(GetNewSignaturesFromTable))
-
Return DV
-
-
End Function
So when I need the list of new signatures (in this case) all I do is
-
Dim DV As DataView = DF.GetNewSignatures
I know, I know, the code could be faster if I just returned the DataView from GetNewSignaturesFromTable, but I like to have options when working with Tables, sometimes I use the same function for different purposes and those different purposes mean that sometimes I will use a DataTable and sometimes a DataReader, so I just return a reader, which is the common denominator, and deal with the rest with those functions.



April 29th, 2008 at 12:53 am
Hi great article. I completely agree that it's a bad habit to return a DataReader.
But to save some time, the conversion from DataReader to DataTable nearly exists in .Net, you only need to a few lines of code.
Check this article: http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=628
It's in C# but can easily be converted to VB.Net