|
-
Dec 29th, 2008, 08:34 PM
#1
ADO.NET Data Containers: An Explanation
This is not actually code for anything but rather an explanation of the ADO.NET classes that store data and how they relate to each other. I think a lot of people don't really understand fully so I thought this was in order.
DataSet
The DataSet is basically an in-memory representation of a database. It doesn't have to match the schema of any actual database and the data it contains doesn't have to even come from a database, but that's its basic purpose.
Just as a database contains tables and relations between them, a DataSet contains DataTables and DataRelations between them. The DataSet's Tables property is a DataTableCollection that contains the DataTables and the Relations property is a DataRelationCollection that contains the DataRelations.
A lot of people use a DataSet without thinking when they don't really need to. A DataSet is a component, which means it can be added to a form in the designer. It's also a convenient way to store multiple DataTables and is essential if you want DataRelations between the tables. If you're just populating a single DataTable in code though, a DataSet is pointless. The Fill method of a DataAdapter can populate a DataTable in a DataSet or a loose DataTable. Use the loose DataTable unless there's a good reason to use a containing DataSet.
DataTable
Just as the DataSet represents a database, a DataTable represents a database table. It doesn't have to match the schema of an actual table though. It basically contains the result set of a query. That query might get all records and all columns of a single table or it might get some columns and some rows from several tables.
The DataTable itself is a container too. Its Columns property is a DataColumnCollection that contains DataColumns, which describe the data in the table, while its Rows property is a DataRowCollection that contains DataRows, whose fields contain the actual data.
Just like a database table, a DataTable has a PrimaryKey. This is an array containing a subset of its DataColumns whose values uniquely identify each record.
The DataTable also has a DefaultView property that contains a DataView. This is similar to the Rows property but not the same. A DataView allows sorting, filtering and transactional editing of the data in the DataTable and will be discussed later.
DataRelation
A DataRelation defines a relationship between two DataTables. It contains a reference to each table and also the columns that are related. A DataRelation can be used to navigate data from a parent table to a child or from a child table to a parent. It can also be used to enforce foreign key constraints on child data and propagate changes from parent to child.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|