473,654 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2892
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)("myf irstItem")

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(Dat aRow dataRow)
{
Customer customer = new Customer();
customer.Custom erID = (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(Sql DataReader dataReader)
{
Customer customer = new Customer();
customer.Custom erID = (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******** ******@tk2msftn gp13.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)("myf irstItem")

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******** ******@tk2msftn gp13.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
1308
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 instance of SqlCommand (slqCommand) setup with the correct connectionstring and have the following peace of code: -------------------------- sqlCommand.CommandText = "select * from t1"; SqlDataReader reader = sqlCommand.ExecuteReader(...
3
9368
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 outs with datasets and datatables (and navigating through the filled datatable)... Just when I thought I had gotten the hang of it, another problem arose: I can't seem to access the "xsi:type" attribute. That is, the XML file looks
1
318
by: Ghost | last post by:
Can I get record as DataRow object from SqlDataReader (if yes how)?
3
3104
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
8363
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 fails. Second, why the data reader is reading every other record. Here is all of the source code for my little application followed by the contents of the log file that it dumps out:...
5
2215
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()) Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9)) Console.Write(myDataReader.GetString(2) + " " + myDataReader.GetString(1) + Chr(9)) Console.Write(myDataReader.GetString(3) + Chr(9)) if (myDataReader.IsDBNull(4)) then
3
2091
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 columns returned by the query. However, I need to be able to determine the size of the varchar fields that are returned by the query. The obvious answer appeared to be to check the DataColumn.MaxLength property. However, after using the Fill method,...
0
2187
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 CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under invalid column. Example is shown below: A part of CSV file. I am reading the first row as...
2
1215
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 many of them use dataset and datarow to retrieve datas . My question is can we use sqldatareader and will it be feasible. My Team Leader says that if you once call it you will not be able to access it in later transactions. Is this true? I will...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1592
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.