By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Reading SqlDataReader or DataRow

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.