Filling a Typed DataSet using the MS Enterprise Library DAAB


Seems this topic causes some confusion, considering the number of posts I’ve seen asking how its done. The documentation doesn’t make it obvious, indeed I don’t think there’s an example in the documentation. Anyway a trawl of the DAAB class reference documentation offers some solutions.

Target environment: MS Enterprise Library June 2005, .NET v1.1, VS.NET 2003

Filling a typed DataSet using can be accomplished in a number of ways, whilst at the same time maintaining provider independence…

1). Filling a Typed DataSet (relinquishes control of connection to DAAB)

public SomeTypedDataSet GetTypedDatasetType1()
{
   Database db = DatabaseFactory.CreateDatabase();
   DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(“SELECT * FROM TABLE_NAME”);
   SomeTypedDataSet ds = new SomeTypedDataSet();
   db.LoadDataSet(cmd, ds, “TABLE_NAME”);
   return ds;
}

2). Filling a Typed DataSet (relinquishes control of connection to DAAB)

public SomeTypedDataSet GetTypedDatasetType2()
{
   Database db = DatabaseFactory.CreateDatabase();
   DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(“SELECT * FROM TABLE_NAME”);
   DataSet uds = db.ExecuteDataSet(cmd);

   SomeTypedDataSet ds = new SomeTypedDataSet();
   uds.Tables[0].TableName = ds.TABLE_NAME.TableName;
   ds.Merge(uds);
   return ds;
}

3). Filling a Typed DataSet (retaining control of connection, for connection reuse perhaps)

public SomeTypedDataSet GetTypedDatasetType3()
{
   SomeTypeDataSet ds = null;
   Database db = DatabaseFactory.CreateDatabase();
   using( IDbConnection conn = db.GetConnection() )
   {
      using( DbDataAdapter da = db.GetDataAdapter() )
      {
         DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(“SELECT * FROM TABLE_NAME”);
         cmd.Command.Connection = conn;
         ((IDbDataAdapter)da).SelectCommand = cmd.Command;

         conn.Open();
         ds = new SomeTypeDataSet();
         da.Fill(ds, “TABLE_NAME”);
      }
      conn.Close();
   }
   return ds;
}

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

2 thoughts on “Filling a Typed DataSet using the MS Enterprise Library DAAB

  1. Is it possible to use DAAB with strongly typed DataSets generated by Visual Studio?

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.