Reading SqlDataReader or DataRow

M

Michael Carr

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
 
C

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
 
M

Michael Carr

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
 
C

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top