DataSet are usually filled using a DbDataAdapter and a DbCommand, however there are some advantages in filling the DataSet using the Microsoft Data Application Block. Note that I don’t use specific objects like SqlDataAdapter or SqlCommand because these objects are specific to SqlServer.

By using the Data Application Block you can delegate it the initialization task of specific database objects, so you can easily change later your data base management system without changing any code. By other hand, if you prefer using the DbDataAdapter you have to initialize the DataSet before filling it and then you need to create a DbConnection object. These three additional steps represent a higher risk to make mistakes, so it’s better filling DataSets using the Microsoft Data Application Block.

The next sample solves a common requirement in back-end (administration) systems. Imagine you have built a Web Application and you want your administrator users can query the data directly. In this sample scenario using DataSets is a good option because you don’t know the structure/schema of the result set. In the following code a SQL select statement is used to fill a DataSet without a DbDataAdapter (or SqlDataAdapter).

static DataTable ExecuteQuery(string query)
 Microsoft.Practices.EnterpriseLibrary.Data.Database database =
 DbCommand command = null;
 DataSet dsResults = null;
 DataTable dtResults = null;

 command = database.GetSqlStringCommand(query);
 dsResults = database.ExecuteDataSet(command);
 if (dsResults != null && dsResults.Tables.Count > 0)
 dtResults = dsResults.Tables[0];

 if (command != null)
 if (dsResults != null)

return dtResults;

The previous sample code can be used by a statement like: ExecuteQuery(“SELECT * FROM CALENDAR”); .Take into account that this is a pretty basic sample, so no security guidelines were followed.

[Update 30/May/2013: it’s a common error among developers opening/closing the data base connection but is not necessary because the Adapter does it automatically]


Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer