Programmatically create data objects

Today is the great DataSet day, which means I hope that I can revise and publish all the following objectives from the syllabus:

  • Programmatically create data objects
  • Work with untyped DataSets and DataTables
  • Expose a DataTableReader form a DataTable or from a DataSet
  • Work with strongly-typed DataSets and DataTables

So much for announcements, let’s dive into the magical world of DataSets (and how to create them programmatically). As you could guess, creating a DataSet takes a single line of code, which is the following:

DataSet ds = new DataSet();

We have one overload of the constructor, which accepts a string as a parameter, and can be used to specify the name of the yet-to-be instantiated DataSet. A little hierarchy: on the top, lives the DataSet. It can have multiple (unlimited) DataTables, and can define any number of DataRelations between these DataTables. DataTables can be further divided to DataRows, DataColumns, and Constraints. They also expose a set of properties, which help you to control the primary keys, DataViews, etc. of them. Let’s not waste more time, and create a full-blown DataSet with two DataTables, a DataRelation, and the rest:

DataSet ds  = new DataSet();
DataTable dtProduct = new DataTable(“Products”);
DataColumn dcProductID = new DataColumn(“ProductID”, typeof(int));
DataColumn dcProductName = new DataColumn(“ProductName”, typeof(string));
DataColumn dcAuthorID = new DataColumn(“AuthorID”, typeof(int));
DataColumn[] theColumns = new DataColumn[] { dcProductID, dcProductName, dcAuthorID };
dtProduct.PrimaryKey = new DataColumn[] { dcProductID };
dtProduct.Constaints.Add(new UniqueConstaint(dcProductName));
DataTable dtAuthor = new DataTable(“Authors”);
DataColumn dcAuthor_ID = new DataColumn(“AuthorID”, typeof(int));
DataColumn dcAuthorName = new DataColumn(“AuthorName”, typeof(string));
dtAuthor.PrimaryKey = new DataColumn { dcAuthor_ID };
ds.Relations.Add(“myRelation”, dtAuthor.Columns[“AuthorID”], dtProduct.Columns[“AuthorID”]);

And that’s all! A fully functional DataSet! But there’s little chance you’d do this, and imagine the same with twenty tables. Bogus, isn’t it? More likely, you’ll define a DataAdapter and a DataSet, and call the DataAdapter’s Fill method to push data and schema into the DataSet.

To populate this DataSet, you’d need the help of DataRows. The DataRow class is an encapsulated and object-oriented record, it represents a single line of data. You can create DataRows by calling the NewRow method of a DataTable, like the following code:

DataRow dr = dtProduct.NewRow();
dr.ItemArray = new object[] {1, “myProduct”, 1 };

We could have also used the Load method of the DataTable class. This has a catch: you must set the primary key before using this method, and provide a member of the LoadOption enumeration. The members are as follows:

  • OverwriteChanges: overwrites the original and the current DataRowVersion to Unchanged.
  • PreserveChanges: the default value. Overwrites the default DataRowVersion, but leaves the current one as-is.
  • Upsert: overwrites the current version, but doesn’t modify the original one.

To understand the enumeration above, you should know that DataRows can have five states, namey: Detached, Added, Unchanged, Modified, Deleted. The state of the DataRow is set when you call the AcceptChanges method of the DataTable/DataSet. Now what’s the big deal with these DataRows states anyway – you could ask. Knowing the state of a DataRow can save you processing time and database-server resources. When you are ready to send back the modified data to your data source, you can simply call the GetChanges method of your DataTable. This method returns only the changed data (DataRows marked with Modified, Added, etc.), in the form of a brand new DataTable with the original schema. Then you can write back this DataTable, containing only the changed values, instead of the whole one.

I’ve mentioned DataViews in the beginning, so let’s take a closer look on them. These DataViews creates windows to DataTables, allowing you to view your data in many different ways (filtering, sorting) without the need of reload it from the data source.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s