Filling a DataTable with a DataReader

G

Guest

Hi,
I am trying to fill a read-only datatable with a datareader in ADO.NET 1.1.
I am filling an ArrayList with the datareader, but having a heck of a time
trying to figure out how to populate the datatable with my ArrayList.

Code so far:

Dim dbRecordsHolder As New ArrayList
Private WithEvents dt As New DataTable("dtDaysClaims")


Private Sub DayEndClaimReviewForm_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load

Me.cnSA.ConnectionString = strConn

Try

Me.sqlCmdGetDaysClaims.Parameters(1).Value = Today
Me.cnSA.Open()
Dim sqlDr As SqlDataReader =
Me.sqlCmdGetDaysClaims.ExecuteReader(CommandBehavior.CloseConnection)

If sqlDr.HasRows Then
For Each rec As IDataRecord In sqlDr
Me.dbRecordsHolder.Add(rec)
Next rec
End If

This successfully populates my ArrayList. I am then trying to use either
DataTable.BeginLoadData() and LoadDataRow(value() As Object, AcceptChanges As
Boolean), or the typical NewRow --> AddRow methods. To iterate through my
ArrayList, I have tried both .GetEnumerator and .MoveNext, as well as using
the ArrayList(index As Int32) with a For i as Int32 = 0 to
Me.dbRecordsHolder.Count - 1. Both methods return an Object, which I am
unsuccessfully trying to cast as either a DataRow or an Object(), to fit my
methods of populating the DataTable mentioned above. I know I could just use
a DataAdapter (in fact, I currently am doing just that). However, my app
populates SEVERAL of these lookup type, read-only DataTables, and I am
looking to be more efficient.
Thanks in advance for any advice.
 
G

Guest

Hi Kevin and Miha,
Thanks for your replies. As I said in my post, I currently am using the
DA.Fill method. I was just wondering if performance would be better and
overhead lower if I could implement a "Fill DataTable from DataReader" method
when populating several of these large read-only datatables. I realize that
from a coding perspective, using DataAdapter.Fill(myDataTable) is certainly
easier.
 
K

Kevin Yu [MSFT]

Hi JT,

Actually, the SqlDataAdapter.Fill() method is using the SqlDataReader to
get all data internally.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top