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

general DataTable questions

P: n/a
Hello everyone,

I have some general questions about the DataTable object, and how it
works. Moderately new to C#, I have plenty of texts describing the
language, but not so much to reference ADO.NET objects (only the MSDN
help files).

I have written a C# Class Library that is responsible for encapsulating
database information. All the objects work just fine for singleton
record insert, update, select, and delete operations. But now we are
looking at interacting with sets of records.

So my plan is to implement a List object for each of the types that I
have a singleton object for. So corresponding to MyObject would be
MyObjectList.

Now I have lots of options of how to handle MyObjectList, and this is
where my questions about the DataTable come into play. In general I
know that each list will have a set of criteria properties, and methods
that can be used to get a set of records from the database. But as for
how to store those in memory ... here's what I've come up with so far:

1) I could simply make a member property of the MyObjectList class as
an array of MyObject classes, built from results of the database call.
Should work just fine, but doesn't seem to be taking advantage of all
the spiffy .NET interoperability (such as DataBinding).

2) I could simply keep a pointer to an SqlDataReader of the information
from the database call. The caller would be responsible for reading
through the records, storing it, and then calling some kind of
Connection Cleanup function on the MyObjectList class to clean up all
the connections that had to stay open while the data was being read.
Don't like this one, simply because it keeps resources tied up for an
indeterminate period of time.

3) I could read all the data out of the DataReader into a DataTable
member of the MyObjectList class. And herein lay my questions. How does
the DataTable class actually work? From samples I've seen where it can
be used to both read and write information to the database table, it
seems like there is some kind of persisted, underlying connection
between the DataTable and the database? Is this true? Does this operate
like a cursor then, where the database records are considered locked in
some fashion while the DataTable exists?

In general I want a set of data to be read-only, like a report. Records
can be updated, but only one at a time, and they would be saved
individually, separately from the list (which is handled perfectly by
the List-As-Array-of-Classes approach). This is similar to what I've
seen in some examples where a DataGrid control is DataBound to a
DataTable, and there is an EditItem command to update a single row.
Would this have similar results to what I've described earlier in this
paragraph?

Any tips or suggestions on how to handle sets of records in this fairly
simple model would be greatly appreciated. I'm sure I didn't include as
much information as you each might need, but it's abstract enough that
I wasn't sure what would be useful. Just let me know if there's any
questions.

Thanks a lot for any help,

Jason

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ng
Jason,

A datatable by itself is not connected to the underlying database. It's
almost like using the old recordset object without a connection object
still open. You can populate a dataset, which holds the datatable, work
with the data in your program, and then use an update or append query
for the database portion. Unfortunately, this is the only way I've used
datasets - I don't like having a persistent open connection.

If you want to really see how ado.net functions, O'Reilly has a great
book called ADO.NET Cookbook.

Tom
jason wrote:
Hello everyone,

I have some general questions about the DataTable object, and how it
works. Moderately new to C#, I have plenty of texts describing the
language, but not so much to reference ADO.NET objects (only the MSDN
help files).

I have written a C# Class Library that is responsible for encapsulating
database information. All the objects work just fine for singleton
record insert, update, select, and delete operations. But now we are
looking at interacting with sets of records.

So my plan is to implement a List object for each of the types that I
have a singleton object for. So corresponding to MyObject would be
MyObjectList.

Now I have lots of options of how to handle MyObjectList, and this is
where my questions about the DataTable come into play. In general I
know that each list will have a set of criteria properties, and methods
that can be used to get a set of records from the database. But as for
how to store those in memory ... here's what I've come up with so far:

1) I could simply make a member property of the MyObjectList class as
an array of MyObject classes, built from results of the database call.
Should work just fine, but doesn't seem to be taking advantage of all
the spiffy .NET interoperability (such as DataBinding).

2) I could simply keep a pointer to an SqlDataReader of the information
from the database call. The caller would be responsible for reading
through the records, storing it, and then calling some kind of
Connection Cleanup function on the MyObjectList class to clean up all
the connections that had to stay open while the data was being read.
Don't like this one, simply because it keeps resources tied up for an
indeterminate period of time.

3) I could read all the data out of the DataReader into a DataTable
member of the MyObjectList class. And herein lay my questions. How does
the DataTable class actually work? From samples I've seen where it can
be used to both read and write information to the database table, it
seems like there is some kind of persisted, underlying connection
between the DataTable and the database? Is this true? Does this operate
like a cursor then, where the database records are considered locked in
some fashion while the DataTable exists?

In general I want a set of data to be read-only, like a report. Records
can be updated, but only one at a time, and they would be saved
individually, separately from the list (which is handled perfectly by
the List-As-Array-of-Classes approach). This is similar to what I've
seen in some examples where a DataGrid control is DataBound to a
DataTable, and there is an EditItem command to update a single row.
Would this have similar results to what I've described earlier in this
paragraph?

Any tips or suggestions on how to handle sets of records in this fairly
simple model would be greatly appreciated. I'm sure I didn't include as
much information as you each might need, but it's abstract enough that
I wasn't sure what would be useful. Just let me know if there's any
questions.

Thanks a lot for any help,

Jason

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.