Data Access Application Blocks
The Data Access Application Block (DAAB) is designed to allow developers to replace ADO.NET boiler-plate code with standardized code for everyday database tasks. The overloaded methods in the Database class can:
If your application needs to address specific DBMS functionality, you can use the PSQL ADO.NET Data Provider.
When Should You Use the DAAB?
The DAAB includes a small number of methods that simplify the most common methods of accessing a database. Each method encapsulates the logic required to retrieve the data and manage the connection to the database. You should consider using the application block if your application uses standard data access techniques.
The DAAB is used with ADO.NET, increasing efficiency and productivity when creating applications for ADO.NET. The abstract Database class provides a number of methods, such as ExecuteNonQuery, ExecuteReader, and ExecuteScalar, that are the same as the methods used by the DbCommand class, or, if you are using database-specific code, a data provider-specific class such as PsqlCommand.
Although using the default DAAB during development is convenient, the resulting application lacks portability. When you use the provider-specific PSQL DAAB implementation, the application includes the PSQL ADO.NET Data Provider’s SQL leveling capabilities. You have more flexibility, whether your application needs to access multiple databases, or whether you anticipate a change in your target data source.
Should You Use Generic or Database-specific Classes?
The application block supplements the code in ADO.NET that allows you to use the same code with different database types. You have two choices when using the DAAB with the ADO.NET data provider:
The GenericDatabase class option is less suited to applications that need specific control of database behaviors. For portability, the provider-specific solution is the optimal approach.
If your application needs to retrieve data in specialized way, or if your code needs customization to take advantage of features specific to PSQL, using the PSQL ADO.NET Data Provider might suit your needs better.
Configuring the DAAB
Before you can configure the DAAB for use with your application, you must set up the environment:
1
2
Open the PSQL DAAB project for your data provider, located in install_dir\Enterprise Library\Src\CS\Pervasive.
3
Configuring the Data Access Application Block consists of two procedures:
Adding a New DAAB Entry
Now, use the Enterprise Library Configuration Tool to add a new DAAB entry:
1
Right-click Enterprise Library Configuration, and select New Application.
2
Right-click Application Configuration, then select New > Data Access Application Block. The Enterprise Library Configuration window appears.
3
4
5
6
Right-click Custom Provider Mappings and select New > Provider Mappings.
7
8
Select the TypeName field, and then choose the Browse button to navigate to the Debug output directory of the PSQL DAAB that you want to build. Then select the TypeName. For example, the PSQL TypeName is Pervasive.EnterpriseLibrary.Data.Pervasive.dll.
9
Adding the Data Access Application Block to Your Application
To add the DAAB to a new or existing application, perform these steps:
1
2
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
3
4
5
Right-click the connection string under the Application Configuration node and select Save Application.
6
7
8
9
10
Using the DAAB in Application Code
Now that you have configured the DAAB, you can build applications on top of this DAAB.
In the example below, we use the DAAB MyPSQL and the DatabaseFactory to generate an instance of a Database object backed by a PSQL data source.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
 
namespace DAAB_Test_App_1
{
class Program
{
static void Main(string[] args)
 
{
Database database = DatabaseFactory.CreateDatabase("MyPSQL");
DataSet ds = database.ExecuteDataSet(CommandType.TableDirect, "SQLCOMMANDTEST_NC_2003SERVER_1");
}
}
}
The Microsoft Enterprise Library DAAB coding patterns are now at your disposal.