Tuesday, April 17, 2007

RSS Reader: Day 4

No progress today on the project itself. After my last post, I got emails telling me that I should be using Typed DataSets. I thought, "what are they and why should I use them?", so today was a research day to figure out what they are.

The first thing I found was that there is a real lack of information about them. I looked in several books on VS 2005 and they either didn't mention Typed DataSets (TDS) or glossed over them. I did finally find some information in one book, but it only gave half of an example and even that wasn't explained well.

I also didn't have lots of luck with web searches. I found some examples that explained them, but from the perspective of moving from VS 2003 to VS 2005. The examples I did find also showed two tables in a parent-child relationship. A Windows form or Web form were then created and the tables were hooked up to standard UI controls. I wanted something simpler.
Before getting into the code, I want to explain a Typed DataSet.

In VFP, we're used to referring to a field as Table.Field. For example, Authors.au_fname. The way I coded my RSS application, the fieldname was a string. So, imagine if in VFP, you treated it like an array, referencing the field either by its index or name. You'd have something like authors(1) or authors("au_fname"). If the field order changed, you'd have to rewrite the code. Or if there was a typo in the fieldname, you wouldn't know until runtime. (Ignore the fact that in VFP, you wouldn't know until runtime anyway.)

With a TDS, you know right away that the fieldname is wrong. It moves errors from runtime to compile time. With a TDS, a class is created that knows all the fieldnames and their datatypes and you can use syntax similar to what we're used to in VFP.

  1. I finally got a simple example of reading data with a TDS working. Here's what you need to do:
    In your VS project, right click on the project and select Add -> New Item. The Add New Item dialog will appear.
  2. Select DataSet and name it (I chose Authors because I'm using the Pubs database) then click Add.
  3. Drag and drop the Authors table from the Pubs database from the Server Explorer onto the design surface. This will create the Authors TDS and the AuthorsTableAdapter.
  4. Right-click on the the AuthorsTableAdapter and select Configure. I kept all the default settings and just clicked through the wizard.

The file Authors.xsd and three children files, Authors.designer.cs, Authors.xsc, and Authors.xss were added to the project. Here is the console application code I wrote to list the author's names.


using System;
using System.Collections.Generic;
using System.Text;
using Pubs.AuthorsTableAdapters;
namespace Pubs
{
class Program
{
static void Main(string[] args)
{
authorsTableAdapter taAuthors = new authorsTableAdapter();
taAuthors.Connection.ConnectionString =
Pubs.Properties.Settings.Default.pubsConnectionString;
Authors.authorsDataTable authors = taAuthors.GetData();

foreach (Authors.authorsRow row in authors)
{
Console.WriteLine("{0} {1}", row.au_fname, row.au_lname);
}
Console.ReadLine();
}
}
}

There are still some questions running around in my head. How do I insert, update, and delete a record? How do I update the TDS schema when I modify the table schema? I've been told that contrary to all the examples, there should only be one table per TDS. How do I then relate a parent to its child table? Some people have told me that they use a TDS, but without the TableAdapter. How do I do that? Other people, and some links I read, say that a TDS and a business object are mutually exclusive. Is that true? If so, which should I use?

I'm in meetings about half the day today, so I won't make much progress in resolving these questions.


Comments: Post a Comment



Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]