Newbie: Working with datasets

  • Thread starter Thread starter John Spiegel
  • Start date Start date
J

John Spiegel

Hi All,

I'm trying to figure out the best way to use ADO.NET to pull in data from an
OLEDB datasource, edit it programatically and return it to the backend. A
more specific issue is that I'm sure there must be a way to traverse through
the records and set various fields, but I'm not yet seeing how to reference
the fields or position to any records. All the examples I've run across so
far seem to assume the changes are coming from user interaction on a web
page.

The current language of choice is C#, which I'm still getting used to, but
any ideas or examples are welcome!

TIA,

John
 
There are a few ways to do this. ADO.NET can be used
in 'connected' mode as well as disconnected mode. In
connected mode, you'd use a DataReader to accomplish what
you want. However, a DataReader can be scrolled through,
it can only be iterated. Simlarly, you can't use it to
do anything but read data so it does nothing for
updates...you'll use NextResult or while dr.Read(){} to
progress through it.

ON the other hand, you can use the disconnected features
employing either DataTables, DataSets or both. A
DataTable is a local copy of a table based on your
query. A DataSet is composed of DataTables but you can
do what you want with just a datatable. So say you
populate your DataTable via a DataAdapter and you want
to iterate through it.... or move around in it...

Datarow d = new DataRow();

foreach d in myDataTable.Rows{ do something;}

Simiarly, you can reference any row you want with it's
index..... myDataTable.Rows[someNumber] You can
reference any column by using the same logic, just adding
a column reference. myDataTable.Rows[someNumber].Columns
[1]. You can use the name of the field instead of a
numeric column reference, so if you had a Query SELECT
First_Name, Last_Name from myTable....you could reference
it by myDataTable.Rows[someNumber].Columns["First_Name"]

I highly recommend using indexes though otherwise
the .NET needs to resolve the index each time you go
through a row and it slows things down. You can use the
GetOrdinal method though if you must use proper names.

Hopefully this helps.

Good Luck,

Bill

W.G. Ryan
(e-mail address removed)
www.knowdotnet.com
 
THANK YOU, BILL! This was exactly what I was having trouble finding.
Nothing like picking up a new language to humble a person!

- John

William Ryan said:
There are a few ways to do this. ADO.NET can be used
in 'connected' mode as well as disconnected mode. In
connected mode, you'd use a DataReader to accomplish what
you want. However, a DataReader can be scrolled through,
it can only be iterated. Simlarly, you can't use it to
do anything but read data so it does nothing for
updates...you'll use NextResult or while dr.Read(){} to
progress through it.

ON the other hand, you can use the disconnected features
employing either DataTables, DataSets or both. A
DataTable is a local copy of a table based on your
query. A DataSet is composed of DataTables but you can
do what you want with just a datatable. So say you
populate your DataTable via a DataAdapter and you want
to iterate through it.... or move around in it...

Datarow d = new DataRow();

foreach d in myDataTable.Rows{ do something;}

Simiarly, you can reference any row you want with it's
index..... myDataTable.Rows[someNumber] You can
reference any column by using the same logic, just adding
a column reference. myDataTable.Rows[someNumber].Columns
[1]. You can use the name of the field instead of a
numeric column reference, so if you had a Query SELECT
First_Name, Last_Name from myTable....you could reference
it by myDataTable.Rows[someNumber].Columns["First_Name"]

I highly recommend using indexes though otherwise
the .NET needs to resolve the index each time you go
through a row and it slows things down. You can use the
GetOrdinal method though if you must use proper names.

Hopefully this helps.

Good Luck,

Bill

W.G. Ryan
(e-mail address removed)
www.knowdotnet.com
-----Original Message-----
Hi All,

I'm trying to figure out the best way to use ADO.NET to pull in data from an
OLEDB datasource, edit it programatically and return it to the backend. A
more specific issue is that I'm sure there must be a way to traverse through
the records and set various fields, but I'm not yet seeing how to reference
the fields or position to any records. All the examples I've run across so
far seem to assume the changes are coming from user interaction on a web
page.

The current language of choice is C#, which I'm still getting used to, but
any ideas or examples are welcome!

TIA,

John


.
 
Back
Top