Filling a DataSet with the Data Application Block

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 =
 DatabaseFactory.CreateDatabase("DbContext");
 DbCommand command = null;
 DataSet dsResults = null;
 DataTable dtResults = null;

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

finally
 {
 if (command != null)
 command.Dispose();
 if (dsResults != null)
 dsResults.Dispose();
 }

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]

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer
About these ads

One response to this post.

  1. [...] Filling a DataSet with the Data Application Block [...]

    Responder

Deja un comentario

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

Seguir

Recibe cada nueva publicación en tu buzón de correo electrónico.

Únete a otros 234 seguidores

%d personas les gusta esto: