UnTyped DataSets and Strongly
Type DataSets
Introduction:
We all are use datasets as a means of carrier
of data from one layer to another. Most of the time we are using weakly typed
datasets. In this article I will explain the differences between weakly typed
datasets and strongly type datasets.
Untyped DataSets
Untyped datasets are hard to maintain since the
developer has to know the in depth details of the database. This scenario is not
practical since in actual programming the developer does not have any idea of
the database layer and its hidden from him by the database administrators.
Another disadvantage of using Untyped DataSets
is that the code looks hideous. Consider the code below which simply retrieves
the first row of the first column from the database.
| // Weak Type
DataSet
DataSet weakDataSet =
new
DataSet();
ad.Fill(weakDataSet,"Articles");
string
articleId = weakDataSet.Tables["Articles"].Rows[0][0].ToString(); |
I am simply make a dataset object and filling
the dataset using SqlDataAdapter. Once the dataset is loaded with data I am
retrieving the first row of the first column in the dataset. As you can see that
by just looking at the code to retrieve the data one cannot simply understand
that what is going to be returned.
Now if you are using a dataset and need to
print all the rows than you might have to code something like this:
| foreach(DataTable
dt in weakDataSet.Tables)
{
foreach(DataRow
dr in dt.Rows)
{
foreach(DataColumn
dc in dt.Columns)
{
myList.Items.Add(dr[dc].ToString());
}
}
} |
As you can see the code above is hard to
understand since we have not mentioned any column in the database. Although it
will work fine but its very hard to understand that what is going on.
Most of these problems are solved by using
Strongly Typed DataSets. Let's see how we can use them.
Strongly Typed DataSets:
You can start using Strongly Typed DataSets by
simply adding a new dataset item from right clicking the project and selecting
add new item. Later you can drag and drop a table from the server explorer to
create a Strongly Typed DataSets.
Once you have created Typed DataSet you can
simply make an object of that dataset and than you are ready to use it. Below I
made an instance of the typed dataset "MyDataSet".
| MyDataSet ds = new
MyDataSet(); |
Once the DataSet is created we need to fill it
using SqlDataAdapter.
Now let's see that how easy it is to refer to
the column and rows that are present in the dataset.
| ds.Articles[0].ArticleID;
ds.Articles[0].Author;
ds.Articles[0].DateCreated; |
As you can see above that now we know that
which columns we are referring to. The Articles is the name of the table and [0]
represents the first row of the column ArticleID, Author and DateCreated.
Now let's see how we can iterate through the
dataset.
| foreach(MyDataSet.ArticlesRow
articleRow in ds.Articles)
{
myList.Items.Add(articleRow.Title);
} |
As you see that now we don't have to type
much code in order to iterate through the dataset. We just used the dataset row
to iterate through the dataset collection and finally we added the items in the
Listbox.
Typed DataSets are more faster than the UnTyped
DataSets. Strongly typed datasets are early bound and their columns represents
the properties. Also that if you are getting errors in Typed DataSets they will
most probably be compile time and not runtime so you can always fix them.
when you make Typed DataSets you also get some
extra features and methods that are very useful.
| ds.Articles[0].IsAuthorNull(); |
As you can see above each column that is
included in the Typed DataSet comes with a method which let's the user know that
if the current row that he is accessing is null or not. This is very convenient
for columns that can have null values. If you don't check for null values
"Object reference not set to an instance" error will be fired.
Adding new columns in the Typed DataSets are
also very easy. Just switch back to the xsd schema graphical user interface and
add the column.

Now after adding a new column just build the
application and you are ready to use the new column.
The coolest feature of using Typed DataSets is
that you can convert the whole dataset schema into a class which can be used by
other users. You can easily do this by using a tool called XSD.exe.
Go to the Visual Studio.NET Command prompt. And
type this command.
xsd MyDataSet.xsd /c
The /c in the end means that we are creating a
class file from the xsd file. There are many things you can do depending on the
type of the escape sequence you apply.
Remember that this command must be run from the
same directory where the xsd file is present. Once you the command it will
create the class file with the same name as that of the xsd file.
Now you schema has become portable and so
others can easily use this.
Many people are using Typed DataSets as
business objects. There are some problems in this approach which I will address
in my future article "Typed DataSets Vs Entity Classes".
I hope you enjoyed the article, happy
programming !