Q

What's the difference between typed dataset and untyped dataset? When do you use which?

What's the difference between typed dataset and untyped dataset? When do you use which? An "untyped" dataset is

an instance of the DataSet class from the System.Data namespace. It's called "untyped" because all columns are provided as the base System.Object type ("object" in C# and "Object" in VB.NET) and must be coerced to the appropriate type. For example:


void Form1_Load(object sender, EventArgs e) {
  DataSet ds = new DataSet();
  sqlDataAdapter1.Fill(ds);
  foreach( DataRow row in ds.Tables[0].Rows ) {
    string fname = (string)row["au_fname"];
    bool contract = (bool)row["contract"];
    string item =
      string.Format("{0} has a contract: {1}", fname, contract);
    listBox1.Items.Add(item);
  }
}

The problem, of course, is that it's a pain to get the coercion code. It is tedious to write and easy to get wrong. A type-safe dataset, on the other hand, allows you to write the following code:


void Form1_Load(object sender, EventArgs e) {
  AuthorsDataSet ds = new AuthorsDataSet();
  sqlDataAdapter1.Fill(ds);
  foreach( AuthorsDataSet.authorsRow row in ds.authors.Rows ) {
    string fname = row.au_fname;
    bool contract = row.contract;
    string item =
      string.Format("{0} has a contract: {1}", fname, contract);
    listBox1.Items.Add(item);
  }
}

In this case, notice that we have a new type, AuthorsDataSet. It derives from the DataSet base type and provides type-safe access to each column in each row via the authorsRow nested type and the authors property on the filled AuthorsDataSet. Now we can simply access the columns as if they were fields without any type coercion code.

There are several ways to get a type-safe DataSet, but the easiest starts with the Solution Explorer (View -> Solution Explorer). The Solution Explorer allows you to explore the SQL Servers you've got on your network as well as establish connections to other OLE DB-compliant data sources. Once you've navigated to the database you're interested in, you can drag a table onto any designer surface, including a WinForm or a WebForm, which will create a connection object and an adapter object, the former for connecting to the database and the latter for filling the DataSet. To generate a type-safe DataSet-derived class, choose Data -> Generate Dataset, entering the name of the type-safe DataSet that you'd like. Since I'm lazy, I prefer to use type-safe DataSet classes all the time, saving me the trouble of writing the type coercion code.

This was first published in October 2002

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close