Creating Data Objects by Hand

In all of the examples so far, you have created the DataSet object and its DataTable and DataRow objects by selecting data from the database. There are, however, occasions when you will want to fill a dataset or a table by hand.

For example, you may want to gather data from a user and then push that data into the database. It can be convenient to add records to a table manually, and then update the database from that table.

The dataset is also an excellent transport mechanism for data. You may even want to create a dataset by hand only to pass it to another tier in your application where it will be used as a data source.

In the next example you will create a dataset and populate three tables by hand. You’ll start by creating theBugs table and specifying its data structure. You’ll then fill that table with records. You’ll do the same for the lkProduct table and the People table.

Once the tables are created, you’ll set constraints on a number of columns, set default values, establish identity columns, and create keys. In addition, you’ll establish a foreign key relationship between two tables, and you’ll create a data relation tying two tables together. It sounds like more work than it really is.

Creating the DataTable by Hand

Start by creating a method named CreateDataSet. The job of this method is to create a DataSet and to populate it by hand, and then to return that resulting DataSet to the calling method, in this case Page_Load.

CreateDataSet begins ...

Get Programming ASP .NET now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.