15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Managing Data <strong>and</strong> relationships: The Dataset Class ❘ 839<br />

When an alteration is made to a column within a DataRow, the ColumnChanging event is raised<br />

on the row’s DataTable. This permits you to override the ProposedValue property of the<br />

DataColumnChangeEventArgs class <strong>and</strong> change it as required. This is one way of performing some data<br />

validation on column values. If you call BeginEdit() before making changes, the ColumnChanging event<br />

will not be raised. This permits you to make multiple changes <strong>and</strong> then call EndEdit() to persist these<br />

changes. If you want to revert to the original values, call CancelEdit().<br />

A DataRow can be linked in some way to other rows of data. This permits the creation of navigable links<br />

between rows, which is common in master/detail scenarios. The DataRow contains a GetChildRows()<br />

method that will return an array of associated rows from another table in the same DataSet as the current<br />

row. These are discussed in the “Data Relationships” section later in this chapter.<br />

schema Generation<br />

You can create the schema for a DataTable in three ways:<br />

➤<br />

➤<br />

➤<br />

Let the runtime do it for you.<br />

Write code to create the table(s).<br />

Use the XML schema generator.<br />

The following sections describe these three alternatives.<br />

Runtime Schema Generation<br />

The DataRow example shown earlier presented the following code for selecting data from a database <strong>and</strong><br />

populating a DataSet class:<br />

SqlDataAdapter da = new SqlDataAdapter(select, conn);<br />

DataSet ds = new DataSet();<br />

da.Fill(ds, "Customers");<br />

This is obviously easy to use, but it has a few drawbacks as well. For example, you have to make do with<br />

the default column names, which might work for you, but in certain instances, you might want to rename a<br />

physical database column (say PKID) to something more user-friendly.<br />

You could naturally alias columns within your SQL clause, as in SELECT PID AS PersonID FROM<br />

PersonTable; it’s best to not rename columns within SQL, though, because a column only really needs to<br />

have a “pretty” name onscreen.<br />

Another potential problem with automated DataTable/DataColumn generation is that you have no control<br />

over the column types that the runtime chooses for your data. It does a fairly good job of deciding the<br />

correct data type for you, but as usual there are instances where you need more control. For example, you<br />

might have defined an enumerated type for a given column to simplify user code written against your class.<br />

If you accept the default column types that the runtime generates, the column will likely be an integer with a<br />

32-bit range, as opposed to an enum with your predefined options.<br />

Last, <strong>and</strong> probably most problematic, is that when using<br />

automated table generation, you have no type-safe access<br />

to the data within the DataTable — you are at the mercy<br />

of indexers, which return instances of object rather than<br />

derived data types. If you like sprinkling your code with<br />

typecast expressions, skip the following sections.<br />

H<strong>and</strong>-Coded Schema<br />

Generating the code to create a DataTable, replete with<br />

associated DataColumns, is fairly easy. The examples<br />

within this section access the Products table from the<br />

Northwind database, shown in Figure 30-6.<br />

figure 30-6<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!