473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C onfigurationSet tings.AppSettin gs("strConn"))
Dim tbcontactsNonAD As DataTable = New DataTable("cont actsNonAD")
Dim cmd As New SqlCommand("Non ADUsers", Myconn)
cmd.CommandType = CommandType.Sto redProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteRead er
ds.Tables("tbco ntactsNonAD").l oad(r)
Myconn.Close()
Nov 19 '05 #1
3 1951
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_hotm ail.com> wrote in message
news:82******** *************** ***********@mic rosoft.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(C onfigurationSet tings.AppSettin gs("strConn"))
Dim tbcontactsNonAD As DataTable = New
DataTable("cont actsNonAD")
Dim cmd As New SqlCommand("Non ADUsers", Myconn)
cmd.CommandType = CommandType.Sto redProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteRead er
ds.Tables("tbco ntactsNonAD").l oad(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(C onfigurationSet tings.AppSettin gs("strConn"))
Dim tbcontactsNonAD As DataTable = New DataTable("cont actsNonAD")
Dim cmd As New SqlCommand("Non ADUsers", Myconn)
cmd.CommandType = CommandType.Sto redProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteRead er
ds.Tables("tbco ntactsNonAD").l oad(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(C onfigurationSet tings.AppSettin gs("strConn"))
Dim tbcontactsNonAD As DataTable = New DataTable("cont actsNonAD")
Dim cmd As New SqlCommand("Non ADUsers", Myconn)
cmd.CommandType = CommandType.Sto redProcedure
Myconn.Open()
Dim r As SqlDataReader = cmd.ExecuteRead er
ds.Tables("tbco ntactsNonAD").l oad(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
1390
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 result to Business Layer. But, considering performace and Efficiency, I have to Close the DataReader Connection.
12
467
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. I find that the second one always is blank. I assume this is because the DataReader is read-forward only. ...
20
5517
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 "clean up" the DataView once it is not referenced and will not effect the number of connections to the database. A DataReader on the other hand...
14
2229
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 should be up to date. However, both the IbuySpy and Duwamish samples and most, if not all, the shopping cart sample codes I've seen use dataset to...
2
1720
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 data to other pages that would call it. I'd return the data as arrays so that I wouldn't have to have my DB connections open all the time while the...
5
4060
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 has a field called id_pasivo, which is foreign key to another table called pasivo, a consecutive field called no_cupon and a third field called...
7
3388
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 internally the DataAdapter uses the DataReader... so whats the deal of writing a method that converts a DataReader into a DataTable ? Thanks!
7
2901
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 returns N rows. DataReader object? Collection object? DataTable object? Returning a DataReader object is not a good practice...right? Thnks for all...
3
8529
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 following questions: 1. Can DataReader be bound DIRECTLY to DataGridView? 2. If DataReader cannot be bound directly to DataGridView, how can I...
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.