473,385 Members | 1,890 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,385 software developers and data experts.

Reading SqlDataReader or DataRow

I have a function that populates a class with values from a database. I'd
like to pass into the function either a SqlDataReader or a DataRow,
depending on which mechanism I'm using to retrieve data from the database.
However, the two classes don't appear to have any common interfaces that
would allow me to enumerate the fields. Yet, when you databind you can pass
either of these classes (as well as many others) and .NET somehow knows how
to get the data out. How can I duplicate this behavior in my function?

Thanks,
Michael Carr
Nov 18 '05 #1
4 2867
Cor
Hi Michael,

I only take the datarow to explain, the sqlDataReader is something I seldom
use and has to check for myself.

A datarow can be a part of a datatable.
A datatable has also columns
A datatable can be a part of a dataset.

I always represent an dataset as
dataset.tables(0).rows(0).item(0).
This is the first item in the first row in the first table of a dataset.

It can also be written as by instance
dataset.tables(0).rows(0)("myfirstItem")

And a lot of other possibilities, but I am not writing a book, I did only
want to get you on the route so you can investigate the rest yourself.

I hope I succeeded a little?

Cor

I have a function that populates a class with values from a database. I'd
like to pass into the function either a SqlDataReader or a DataRow,
depending on which mechanism I'm using to retrieve data from the database.
However, the two classes don't appear to have any common interfaces that
would allow me to enumerate the fields. Yet, when you databind you can pass either of these classes (as well as many others) and .NET somehow knows how to get the data out. How can I duplicate this behavior in my function?

Nov 18 '05 #2
Cor, thank you for your reply but it is not exactly what I am looking for.
Let me try to explain a little more...

Say I retrieve a particular table (Customers) from the database and put it
in a DataTable. Now, I can feed a particular row of that table to a function
and access its data like this:

public Customer GetCustomer(DataRow dataRow)
{
Customer customer = new Customer();
customer.CustomerID = (int)dataRow["CustomerID"];
return customer;
}

Similarly, I could retrieve the table into a SqlDataReader and feed it to an
identical-looking function:

public Customer GetCustomer(SqlDataReader dataReader)
{
Customer customer = new Customer();
customer.CustomerID = (int)dataRow["CustomerID"];
return customer;
}

Since the bodies of the functions are identical, what I would like to do is
merge the two functions into one. The databinding controls do something
similar -- you feed the databinding control either a DataTable or a
SqlDataReader and they somehow instinctively know what to do with it.
However, since DataRow and SqlDataReader have no interfaces in common, there
is no way to treat both cases in the same function...

Michael Carr

"Cor" <no*@non.com> wrote in message
news:Ob**************@tk2msftngp13.phx.gbl...
Hi Michael,

I only take the datarow to explain, the sqlDataReader is something I seldom use and has to check for myself.

A datarow can be a part of a datatable.
A datatable has also columns
A datatable can be a part of a dataset.

I always represent an dataset as
dataset.tables(0).rows(0).item(0).
This is the first item in the first row in the first table of a dataset.

It can also be written as by instance
dataset.tables(0).rows(0)("myfirstItem")

And a lot of other possibilities, but I am not writing a book, I did only
want to get you on the route so you can investigate the rest yourself.

I hope I succeeded a little?

Cor

I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow,
depending on which mechanism I'm using to retrieve data from the database. However, the two classes don't appear to have any common interfaces that
would allow me to enumerate the fields. Yet, when you databind you can

pass
either of these classes (as well as many others) and .NET somehow knows

how
to get the data out. How can I duplicate this behavior in my function?


Nov 18 '05 #3
Cor
Hi Michael,

Maybe it sounds stupid, but for me looks this just like the dataAdapter and
the dataset.

Why would you need that datareader, when a datatable is filled with the
dataadapter with one fill to the dataset that can be bounded in one time
direct to every control.

Cor
Nov 18 '05 #4
I believe that everything ends up back to the IEnumerable interface.

Jeff
"Cor" <no*@non.com> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
Hi Michael,

Maybe it sounds stupid, but for me looks this just like the dataAdapter and the dataset.

Why would you need that datareader, when a datatable is filled with the
dataadapter with one fill to the dataset that can be bounded in one time
direct to every control.

Cor

Nov 18 '05 #5

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

Similar topics

0
by: Morten Overgaard Hansen | last post by:
Hi, I'm using the SqlServer managed provider from the framework (SqlClient) to communicate with SqlServer. I have the following table in the database: create table t1(f1 int) I already have an...
3
by: Carl Lindmark | last post by:
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and...
1
by: Ghost | last post by:
Can I get record as DataRow object from SqlDataReader (if yes how)?
3
by: msnews | last post by:
Hi All, I have the following code ------------------------------------------------ Business.Prod prod = new Business.Prod(); SqlDataReader Product = prod.GetProducts(); while (prod.Read())...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
5
by: Tom Edelbrok | last post by:
I notice that using the SqlDataReader requires the use of ordinal field references rather than by name. For example, do while (myDataReader.Read())...
3
by: Oenone | last post by:
I have a project that creates a SqlDataAdapter and uses its Fill method to fill a DataTable with data from a user-provided query. From there I can obviously access details about the rows and...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
2
by: cmrhema | last post by:
Hi, while going for a project it is customary to create a data access layer(i.e. a class file will be created with all the links such as insert,delete,update etc.) Throughout I have seen that...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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...

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.