During my last presentation at the
Perth .Net Community of Practice on Enterprise Library 2.0 (see this
blog and
here for newly posted code), I was asked if you could use the Data Access Application Blocks (DAAB) without using Enterprise Library configuration data. I indicated that you could and this post explains how.
Regular usage of DAAB and the EL config looks something like this:
Database customerDatabase = DatabaseFactory.CreateDatabase();
IDataReader reader = customerDatabase.ExecuteReader("GetAllCustomers");where the CreateDatabase method will refer to the EL config to find the default database:
<dataConfiguration defaultDatabase="EntLibQuickStarts" />which in turn uses the standard .Net connection strings section (note: new to EL 2.0):
<connectionStrings> ...For configuration-less usage of DAAB there are 2 options for creating a 'Database' instance:
Option 1: Know database type - If you are happy to create a SqlDatabase type because you do not need to support database-agnostic code then use this code (NOTE: you will need to add
using Microsoft.Practices.EnterpriseLibrary.Data.Sql; to your data access code):
SqlDatabase sqlDatabase = new SqlDatabase(mySqlConnectionString);Option 2: Generic database type - If you require database-agnostic code then use this (NOTE: you will need to add
using System.Data.Odbc; to your data access code):
GenericDatabase db = new GenericDatabase(myOdbcConnectionString,OdbcFactory.Instance);
So again, not too much code is required to create the Database instance even when you are not using the standard config of the Enterprise Library.