SSDS strongly-typed DataSets

2008-10-14 @ 02:36#

i got curious this evening and threw together a simple SSDS example that uses strongly-typed DataSets. it's a very simple example, but at least it shows that DataSets and SSDS get along just fine.

i created a valid XSD schema document for my Task entity. I then used XSD.exe to generate a CS class that defines a strongly-typed DataSet. finally, i added this class to a console project and wrote a short bit of code to pull the live list of entities from my SSDS data store, load them into the DataSet, and enumerate them to the console. not bad at all.

-- DOS batch sample
@echo off
rem 2008-10-13 (mca) : make typed datasets from SSDS entity

set d=..\entity-binding
xsd task.xsd sitka.xsd /dataset /n:Amundsen.SSDS.Binding /o:%d%set d=

rem eof

-- C# console sample
static void Main(string[] args)
{
  HttpClient c = new HttpClient();
  NewDataSet ds = new NewDataSet();
  string xml = string.Empty;
  int x = 0;

  try
  {
    // get data from SSDS (this URL doesn't require SSDS auth)
    xml = c.Execute("http://amundsen.com/ssds/tasks/");

    // load the data into the strongly-typed dataset
    using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(xml)))
    {
      ds.ReadXml(ms);
      ms.Close();
    }

    // enumerate the task table
    Console.WriteLine(ds.task.TableName);
    foreach (NewDataSet.taskRow r in ds.task)
    {
      Console.WriteLine(string.Format("{0}: {1}, {2}, {3}, {4}", 
        ++x, r.Id, r.name, r._is_completed, r.Version));
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(string.Format("ERROR: {0}",ex.Message));

  }

  // wait for user to mash the keyboard
  Console.ReadLine();
}

more cool stuff you can do w/ this sample includes:

  • use this to bind to a WinForms DataGrid
  • work up code to handle add, update, and delete events in the DataSet
  • use the filter, sort, and other View options for the DataSet

you can download the sample code from my Codeplex or Google code sites. you can also see more example of my SSDS demos at my SSDS page.

code