The DataTable Class

Team-Fly team-fly    

 
ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 7.   The ADO .NET DataSet


If the DataSet is the heart of ADO .NET, then the DataTable class is the heart of the DataSet. The DataSet object is essentially a collection of DataTable objects. To understand the DataSet is to understand the DataTable. The DataTable class is itself a collection of DataColumn and DataRow objects. The DataColumn class defines the metadata for the DataTable. Each DataColumn object contains properties that define each column in the table. These properties will be the name , data type, size , and other properties that define the column. The DataRow class contains the values of each column by row. The DataRow class is the element of the DataRowCollection. This is a departure from previous versions of ADO where there was a concept of the current row and you navigated the RecordSet using methods such as MoveNext and MovePrevious. With ADO .NET, all rows are available at all times. You can use an indexing variable to access the rows, or you can use an enumerator using the For Each Next construct.

DataTable Properties and Methods

The following methods and properties are defined at the table level. Some are also defined at the DataSet level. When they are invoked at the DataSet level they apply to all of the DataTable objects in the DataSet. When they are invoked at the DataTable level, they only apply to the DataTable they are invoked on.

  • Child and parent relations Collections of Relation objects that point to another table with either a one-to-many relationship to the current table (child) or a many-to-one relationship to the current table (parent). These have to do with master-detail structures. We will look at this later in the chapter.

  • Constraints A collection of constraint objects. There are two kinds of constraint classesunique and foreign key. A unique constraint is used to guarantee that no two rows in a table have the same value in the columns referenced in the unique constraint. A foreign key constraint is used to determine what happens to a row on the right side of the constraint when a row on the left side is changed. Possibilities include cascaded deletes and updates, and referential integrity violations. For example, you cannot delete a parent row if there are any child rows.

  • DefaultView Returns a DataView object that has the default view settings (sorts, filters, and so forth) of the DataTable.

  • AcceptChanges, RejectChanges Tells the DataTable to either accept or cancel any changes that were made since the last call to Accept or Cancel changes.

  • HasChanges Returns True if any data in the DataTable has changed since the last call to Accept or Cancel changes.

  • GetChanges Returns a copy of the DataTable with only those rows that have changed since the last call to Accept or Cancel changes.

  • HasErrors Returns True if any rows in the table have unresolved errors.

  • GetErrors Returns an array of DataRow objects that contain errors.

  • BeginLoadData Turns off index building and constraint checking. This is useful if you are loading a large amount of data into the DataTable and you want to increase performance.

  • EndLoadData Restores indexing and constraint checking after data is loaded. Be sure to check HasErrors for any errors generated and GetErrors to edit and fix them.

DataTable Notifications (Events)

The DataTable also provides the following notifications:

  • ColumnChanging, ColumnChanged Fires when a column is going to change and a column has successfully changed.

  • RowChanging, RowChanged Fires when a row is going to change or has successfully changed.

  • RowDeleting, RowDeleted Fires when a row will be deleted or is successfully deleted.

To receive these events, you must declare a variable of type DataTable and assign the DataTable from the DataSet to it. You must use the WithEvents keyword in the declaration. You can also wire the events to procedures programmatically without declaring a variable like so:

 AddHandler DataSet11.Customers.RowChanged, AddressOf mDT_RowChanged 

Where mDT_RowChanged has this declaration:

 Private Sub mDT_RowChanged(ByVal sender As Object, ByVal e As  System.Data.DataRowChangeEventArgs)           MsgBox("Row Changed")         End Sub 

The contents of the procedure in the example are irrelevant.


Team-Fly team-fly    
Top


ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net