354
Calling this version of the Select method is the same as calling the full version with a recordStates
value of DataViewRowState.CurrentRows
. Similarly, there is a one-parameter version that takes only a
filterExpression :
Public Overloads Function Select _ ByVal filterExpression As String _
As System.Data.DataRow
This is the same as calling the three-parameter version with sort
equal to the empty string and
recordStates equal to
DataViewRowState.CurrentRows .
Lastly, there is the parameterless version of Select: Public Overloads Function Select As System.Data.DataRow
This is the same as calling the three-parameter version with filterExpression
and sort
equal to the empty string and
recordStates equal to
DataViewRowState.CurrentRows .
As an example of using the Select method, this line of code returns all rows whose Country column contains the value
Mexico :
Dim rows As DataRow = dt.SelectCountry = Mexico Because the
sort and
recordStates parameters were not specified, they default to
the empty string and
DataViewRowState.CurrentRows , respectively.
8.5.3.1 The Select method versus the SQL SELECT statement
If an application is communicating with a database over a fast, persistent connection, it is more efficient to issue SQL
SELECT statements that load the DataSet with only the desired records, rather
than to load the DataSet with a large amount of data and then pare it down with the DataTables Select method. The Select method is useful for distributed applications that might not have a fast
connection to the database. Such an application might load a large amount of data from the database into a DataSet object, then use several calls to the DataTables Select method to locally view and
process the data in a variety of ways. This is more efficient in this case because the data is moved across the slow connection only once, rather than once for each query.
8.5.4 Finding Column Values
The DataRow class has an Item property that provides access to the value in each column of a row. For example, this code iterates through all the columns of a row, displaying the value from each
column assume that row
holds a reference to a DataRow object: Iterate through the column values.
Dim n As Integer For n = 0 To row.Table.Columns.Count - 1
Console.WriteLinerown Next
Note the expression used to find the number of columns: row.Table.Columns.Count
. The DataRow objects Table property holds a reference to the DataTable object of which the row is a part.
As will be discussed shortly, the Table objects Columns property maintains a collection of column definitions for the table. The Count property of this collection gives the number of columns in the table
and therefore in each row.
355
The DataRow objects Item property is overloaded to allow a specific column value to be accessed by column name. The following code assumes that the DataRow
row contains a column named
Description. The code displays the value of this column in this row: Console.WriteLinerowDescription
8.5.5 Finding Column Definitions
The DataTable objects Columns property holds a ColumnsCollection object that in turn holds the definitions for the columns in the table. The following code iterates through the columns in the table
and displays their names:
Iterate through the columns. Dim column As DataColumn
For Each column In dt.Columns Console.WriteLinecolumn.ColumnName
Next
This code does the same thing, using a numeric index on the ColumnsCollection object: Iterate through the columns.
Dim n As Integer For n = 0 To dt.Columns.Count - 1
Console.WriteLinedt.Columnsn.ColumnName Next
The ColumnsCollection object can also be indexed by column name. For example, if DataTable dt
contains a column named Description, this code gets a reference to the associated DataColumn object:
Dim column As DataColumn = dt.ColumnsDescription
8.5.6 Changing, Adding, and Deleting Rows
To change data in a DataSet, first navigate to a row of interest and then assign new values to one or more of its columns. For example, the following line of code assumes that
row is a DataRow object
that contains a column named Description. The code sets the value of the column in this row to be Milk and cheese:
rowDescription = Milk and cheese Adding a new row to a table in a DataSet is a three-step process:
1. Use the DataTable classs NewRow method to create a new DataRow. The method takes no parameters.
2. Set the values of the columns in the row. 3. Add the new row to the table.
For example, assuming that dt
is a DataTable object, and that the table has columns named CategoryName and Description, this code adds a new row to the table:
Add a row. Dim row As DataRow = dt.NewRow
rowCategoryName = Software rowDescription = Fine code and binaries
dt.Rows.Addrow
356
The DataRow object referenced by row
in this code can be indexed by the names CategoryName and Description because the DataRow object was created by the DataTable objects NewRow
method and so has the same schema as the table. Note that the NewRow method does not add the row to the table. Adding the new row to the table must be done explicitly by calling the
DataRowCollection classs Add method through the DataTable classs Rows property.
Deleting a row from a table is a one-liner. Assuming that row
is a reference to a DataRow, this line deletes the row from its table:
row.Delete When changes are made to a row, the DataRow object keeps track of more than just the new column
values. It also keeps track of the rows original column values and the fact that the row has been changed. The Item property of the DataRow object is overloaded to allow you to specify the desired
version of the data that you wish to retrieve. The syntax of this overload is:
Public Overloads ReadOnly Property Item _ ByVal columnName As String, _
ByVal version As System.Data.DataRowVersion _ As Object
The parameters are: columnName
The name of the column whose value is to be retrieved. version
The version of the data to retrieve. This value must be a member of the System.Data.DataRowVersion enumeration. Its values are:
Current Retrieve the current changed version.
Default Retrieve the current version if the data has been changed, the original version if not.
Original Retrieve the original unchanged version.
Proposed Retrieve the proposed change. Proposed changes are changes that are made after a call to a
DataRow objects BeginEdit method but before a call to its EndEdit or CancelEdit methods. For more information, see
Sect ion 8.6 later in this chapter.
For example, after making some changes in DataRow row
, the following line displays the original version of the rows Description column:
Console.WriteLinerowDescription, DataRowVersion.Original The current value of the row would be displayed using any of the following lines:
357
Console.WriteLinerowDescription, DataRowVersion.Current Console.WriteLinerowDescription, DataRowVersion.Default
Console.WriteLinerowDescription
Calling the DataSet objects AcceptChanges method commits outstanding changes. Calling the DataSet objects RejectChanges method rolls records back to their original versions.
The code shown in this section affects only the DataSet object, not the data source. To propagate these changes, additions, and
deletions back to the data source, use the Update method of the SqlDataAdapter class, as described in
Sect ion 8.5.7 .
If there are relations defined between the DataTables in the DataSet, it may be necessary to call the DataRow objects
BeginEdit method before making changes. For more information, see
Sect ion 8.6 later in this chapter.
8.5.7 Writing Updates Back to the Data Source