Get the entire row from SqlDataReader

  • Thread starter Thread starter Wilhelm Heramb
  • Start date Start date
W

Wilhelm Heramb

How can I get the entire row (current) from the SqlDataReader;

Some pseudo code:

DataRow row;
while (sqlDataReader.Read())
{
row = sqlDataReader[... and so forth...
dataTable.Rows.Add(row);
}

Above code is what I wish for...but I know it is impossible. Any ideas to
how to solve this and get the current row as a DataRow.


Andreas :-)
 
The GetValues method of the SqlDataReader will accomplish what you want if I
understand your question correctly. However, just as an FYI (I konw this is
psuedo code but if you were trying to actually do this as opposed to simply
illustrating your desire, I figured I'd mention it)., if you're actually
trying to load the values of a datareader into a datatable, it'd be a lot
easier to just use a dataadapter and have it fill the table for you There
are ostensibly some reasons that you may want to use a reader anyway, but in
my experience, the added complexity and code offset any benefit in
performance. However, I'm guessing this was just an example so my point is
irrelevant.

Anyway, hope this helps and if I misunderstood your question, please let me
know and I'll try to address it.

Cheers,

Bill
 
If I am understanding the question, if you are wanting the results from the
datareader in a datarow to attach the row to a datatable read on ...

I have not done this, but, can you not use the DataRow constructor that
takes an array of values and simply pass it the GetValues array?

L
 
I agree with most of the approaches suggested. I've seen any number of very
tight (and some not so tight) routines to convert a DataReader stream to a
DataTable. There are several posted to the web. However, none of them has
outperformed the Fill method. Not only that, but in 80 days you'll be able
to use the DataTable.Load function to import a DataReader directly into a
DataTable or the DataSet.Load method to build multiple tables (from multiple
resultsets) from a single DataReader stream. I would not be re-inventing the
wheel at this point in time.

Consider that every single line of code you write will have to be debugged
and supported from this point forward in time. IMHO, the minuscule savings
in performance you'll achieve by converting a DataReader stream to a
DataTable are not worth the extra expense of having to maintain this code.

hth
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
You can load a DataReader into DataSet with V1.1, its just not obvious how.

(new LoadDataReader()).Fill(dataset, reader);

internal class LoadDataReader : System.Data.Common.DbDataAdapter() {
internal int Fill(DataSet dataset, IDataReader reader) {
// the method on DbDataAdapter that takes a DataSet and a reader is
protected
// so you can only call it if your derive from DbDataAdapter and
delegate to it
base.Fill(dataset, "Table", reader, 0, 0);
}
internal int Fill(DataTable datatable, IDataReader reader) {
// the method on DbDataAdapter that takes a DataSet and a reader is
protected
// so you can only call it if your derive from DbDataAdapter and
delegate to it
base.Fill(table, reader);
}
}

--
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.

William (Bill) Vaughn said:
I agree with most of the approaches suggested. I've seen any number of very
tight (and some not so tight) routines to convert a DataReader stream to a
DataTable. There are several posted to the web. However, none of them has
outperformed the Fill method. Not only that, but in 80 days you'll be able
to use the DataTable.Load function to import a DataReader directly into a
DataTable or the DataSet.Load method to build multiple tables (from
multiple resultsets) from a single DataReader stream. I would not be
re-inventing the wheel at this point in time.

Consider that every single line of code you write will have to be debugged
and supported from this point forward in time. IMHO, the minuscule savings
in performance you'll achieve by converting a DataReader stream to a
DataTable are not worth the extra expense of having to maintain this code.

hth
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

Wilhelm Heramb said:
How can I get the entire row (current) from the SqlDataReader;

Some pseudo code:

DataRow row;
while (sqlDataReader.Read())
{
row = sqlDataReader[... and so forth...
dataTable.Rows.Add(row);
}

Above code is what I wish for...but I know it is impossible. Any ideas to
how to solve this and get the current row as a DataRow.


Andreas :-)
 
Thanks for all the answers that guided me to the solution!
This is how I solved it (sample code based on ADO.NET 2.0):

while (sqlDataReader.Read())
{
//Some other work...

//Oops... we need to log this...
if(Convert.ToString(sqlDataReader["MyColumn"]) == "MyValue")
{
object[] values = new object[sqlDataReader.VisibleFieldCount];
sqlDataReader.GetValues(values);
logTable.LoadDataRow(values, false);
}
}

Note: the table which I add the current row has the same
recordlayout/datatypes as the one beeing red by the datareader.


Andreas :-)
 
Back
Top