Databases Reference
In-Depth Information
3. Make ModifyDataTable the startup project, and run it by pressing Ctrl+F5. You
should see the results in Figure 13-6.
Figure 13-6. Modifying a data table
How It Works
As before, you use a single data table in a dataset.
// get data table reference
DataTable dt = ds.Tables["employees"];
Next, you can see an example of how you can change the schema information. You
select the FirstName column, whose AllowNull property is set to false in the database,
and you change it—just for the purposes of demonstration—to true .
// FirstName column should be nullable
dt.Columns["firstname"].AllowDBNull = true;
Note that you can use an ordinal index (for example, dt.Columns[1] ) if you know what
the index for the column is, but using * to select all columns makes this less reliable since
the position of a column may change if the database table schema changes.
You can modify a row using the same technique. You simply select the appropriate
row and set its columns to whatever values you want, consistent with the column data
types, of course. The following line shows the City column of the first row of the dataset
being changed to Wilmington.
// modify City in first row
dt.Rows[0]["city"] = "Wilmington";
Next you add a new row to the data table.
Search WWH ::




Custom Search