Adding Application Code

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The Data Access Application Block is designed to support the most common scenarios for accessing a database. When you add your application code, refer to the scenarios in the Key Scenarios sections, and select the ones that best match your situation. Use the code that accompanies the scenario as-is or adapt it as necessary.

First, you must prepare your application to use the Data Access Application Block.

To prepare your application

  1. Add a reference to the Data Access Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab, and then navigate to the location of the Microsoft.Practices.EnterpriseLibrary.Data.dll assembly. Select the assembly, and then click OK to add the reference.
  2. Following the same procedure, add references to the Enterprise Library Core assemblies, Microsoft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.ObjectBuilder.dll.
  3. (Optional) To use elements from Data Access Application Block without fully qualifying the element reference, you can add the following using statement (C#) or Imports statement (Visual Basic) to the top of your source code file.
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System.Data;
    
    
    Ff647917.note(en-us,PandP.10).gifNote:
    For Visual Basic projects, you can use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer. On the Project menu, click Properties. When the Project Designer appears, click the References tab.

Creating the Database Object

All data access methods are executed against a Database object. You can use the DatabaseFactory class to create a Database object. The specific type of Database object created by the factory is determined by the application configuration information.

You can use the configuration console to specify a default database instance. The DatabaseFactory creates the database specified by the default instance when you call the CreateDatabase method without passing a database instance name. The following application code shows how to create a Database object for the default instance.

Database db = DatabaseFactory.CreateDatabase();

Alternatively, the application code can specify a named instance of a database. For example, if you used the configuration console to create an instance named "Sales" the code to create a Database object for that specific instance would look like the following.

Database db = DatabaseFactory.CreateDatabase("Sales");

If you know the connection string for the database you want to create, you can bypass the application's configuration information and use a constructor to directly create the Database object. Because the Database class is an abstract base class, you must construct one of its derived types. The derived Database type determines the ADO.NET data provider. For example, the SqlDatabase class uses the SqlClientFactory provider, the SqlCeDatabase class uses the SqlCeProviderFactory provider, and the OracleDatabase class uses the OracleClientFactory provider. It is your responsibility to construct the appropriate type of Database class for the connection string.

The following code creates a SqlDatabase object with the supplied connection string.

// Assume the method GetConnectionString exists in your application and 
// returns a valid connection string.
string myConnectionString = GetConnectionString();

SqlDatabase sqlDatabase = new SqlDatabase(myConnectionString);

If you use a connection string for a data provider other than the ADO.NET SQL data provider or Oracle data provider, you can construct a GenericDatabase object. When you create a GenericDatabase object, you must supply the DbProviderFactory object.

Selecting the Appropriate Method Overloads

Each data access method has multiple overloads. The following descriptions and guidelines can help you to select the appropriate overload:

  • There are overloads that accept an ADO.NET 2.0 DbCommand object. These overloads provide the most flexibility and control for each method.
  • There are overloads that accept a stored procedure name and a collection of values to be used as parameter values for the stored procedure. These overloads are convenient when your application calls stored procedures that have input-only parameters.
  • There are overloads that accept a System.Data.CommandType and a string that represents the command. These are convenient overloads to use when your application executes inline SQL statements or stored procedures without parameters.
  • Finally, each of the preceding overloads includes an overload that accepts a transaction. This allows you to use the desired style of overload while executing the method in an existing transaction.

Each key scenario demonstrates one of the overloads available for a particular method. Many of the scenarios can be completed using other available overloads.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.
Show: