473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DataReader to DataTable...

Can anyone tell me how to achieve the following...

I want to convert data from a DataReader into a DataTable...

I was told this should do it but it doesn't seem to work!

Dim Myconn As New SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim tbcontactsNonAD As DataTable = New DataTable("contactsNonAD")
Dim cmd As New SqlCommand("NonADUsers", Myconn)
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteReader
ds.Tables("tbcontactsNonAD").load(r)
Myconn.Close()
Nov 19 '05 #1
3 1942
Tim::..
It's helpful to us trying to help you if you don't spawn multiple threads on
the same topic....

Just loop through the datareader and create new rows...

while dr.read()
Dim row as DataRow = tbContactsNonAD.NewRow()
row(0) = row(0)
row(1) = row(1)
tbContactsNonAD.Rows.Add(row)
end
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:82**********************************@microsof t.com...
Can anyone tell me how to achieve the following...

I want to convert data from a DataReader into a DataTable...

I was told this should do it but it doesn't seem to work!

Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim tbcontactsNonAD As DataTable = New
DataTable("contactsNonAD")
Dim cmd As New SqlCommand("NonADUsers", Myconn)
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteReader
ds.Tables("tbcontactsNonAD").load(r)
Myconn.Close()

Nov 19 '05 #2
If you are doing this right after the pull, you might as well just use a
DataAdapter and fill a DataSet. It will certainly reduce the amount of work
you have to do and you can strongly type the DataSet to increase perf. Of
course, this is not 100% true if you are in the 2.0 Framework (Visual Studio
2005), as the ADO.NET model has greatly expanded.

As far as "you should not do this", that is an incorrect statement.
Microsoft uses a DataReader to fill their own DataSets (or rather DataTables
in a DataSet). The DataReader serves as a stream of data (or a firehose
cursor, fast forward cursor, or other ways of naming), while a DataTable is
an endpoint. You can bind directly from a reader to controls, but you lose
the interim step and have to manufacture your own return trip. The Reader is
much faster, but less maintainable for round tripping of data. The only
reason I might agree with "you should not" is it is unlikely you will fill a
DataTable with an algorithm superior to Microsoft's. Make sure you are not
spending a lot of time spinning your wheels to reinvent a mousetrap that is
already on the market; beyond that, there is nothing wrong with moving data
from a "stream" to a "container".

With the DataTable in 1.x, you can load a data row with LoadDataRow(), but
this means you have to build the row. You can also do this in 2.0, but that
is all you have for 1.x. With 2.0, you have a Load() method to make it easier
to load from an object that implements IDataReader. Note, however, that
Microsoft's own documenation shows the new DataTableReader being used rather
than a SqlDataReader:
http://msdn2.microsoft.com/library/7...us,vs.80).aspx

For the SqlDataReader, my next line of attack would be setting it up as its
interface, IDataReader, or casting it as DbDataReader, which is a common
ancestor of both SqlDataReader and the new DataTableReader. I would not
necessarily leave it this way after the code goes gold, but I would consider
this to solve the problem today. I am not sure which version of the 2.0
Framework you are working with (June CTP is the latest), but it is possible a
different version will work with this class.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"Tim::.." wrote:
Can anyone tell me how to achieve the following...

I want to convert data from a DataReader into a DataTable...

I was told this should do it but it doesn't seem to work!

Dim Myconn As New SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim tbcontactsNonAD As DataTable = New DataTable("contactsNonAD")
Dim cmd As New SqlCommand("NonADUsers", Myconn)
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteReader
ds.Tables("tbcontactsNonAD").load(r)
Myconn.Close()

Nov 19 '05 #3
If you are doing this right after the pull, you might as well just use a
DataAdapter and fill a DataSet. It will certainly reduce the amount of work
you have to do and you can strongly type the DataSet to increase perf. Of
course, this is not 100% true if you are in the 2.0 Framework (Visual Studio
2005), as the ADO.NET model has greatly expanded.

As far as "you should not do this", that is an incorrect statement.
Microsoft uses a DataReader to fill their own DataSets (or rather DataTables
in a DataSet). The DataReader serves as a stream of data (or a firehose
cursor, fast forward cursor, or other ways of naming), while a DataTable is
an endpoint. You can bind directly from a reader to controls, but you lose
the interim step and have to manufacture your own return trip. The Reader is
much faster, but less maintainable for round tripping of data. The only
reason I might agree with "you should not" is it is unlikely you will fill a
DataTable with an algorithm superior to Microsoft's. Make sure you are not
spending a lot of time spinning your wheels to reinvent a mousetrap that is
already on the market; beyond that, there is nothing wrong with moving data
from a "stream" to a "container".

With the DataTable in 1.x, you can load a data row with LoadDataRow(), but
this means you have to build the row. You can also do this in 2.0, but that
is all you have for 1.x. With 2.0, you have a Load() method to make it easier
to load from an object that implements IDataReader. Note, however, that
Microsoft's own documenation shows the new DataTableReader being used rather
than a SqlDataReader:
http://msdn2.microsoft.com/library/7...us,vs.80).aspx

For the SqlDataReader, my next line of attack would be setting it up as its
interface, IDataReader, or casting it as DbDataReader, which is a common
ancestor of both SqlDataReader and the new DataTableReader. I would not
necessarily leave it this way after the code goes gold, but I would consider
this to solve the problem today. I am not sure which version of the 2.0
Framework you are working with (June CTP is the latest), but it is possible a
different version will work with this class.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"Tim::.." wrote:
Can anyone tell me how to achieve the following...

I want to convert data from a DataReader into a DataTable...

I was told this should do it but it doesn't seem to work!

Dim Myconn As New SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim tbcontactsNonAD As DataTable = New DataTable("contactsNonAD")
Dim cmd As New SqlCommand("NonADUsers", Myconn)
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteReader
ds.Tables("tbcontactsNonAD").load(r)
Myconn.Close()

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: VIJAY KUMAR | last post by:
Hi, Problem: Return ArrayList where Values are Added using DataReader. Note: My Data Reader returns M Columns and N Rows I am using DataReader in Data Access layer. I want to return this...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
14
by: Bihn | last post by:
I was reading about datareader which is said to be slimmer & faster then dataset. Since the datareader have to go fetching the dat from the database every time it need it, the data it gets then...
2
by: Patreek | last post by:
Hi, I'm writing my first real asp.net app at my job, and I'd like opinions please. In my classic ASP apps that I've written, I'd often have separate files for retreiving data and returning the...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
7
by: Varangian | last post by:
Hi all, the question I want to ask if the conversion of a DataReader to a Table looping through the DataReader is better than using the Fill Method of the DataAdapter... I'm asking because...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
3
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to add a datagridview control to a Windows Form to display read-only information in visual basic 2005. My understanding is that datareader will be faster for this purpose. I have the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.