I recently came across a situation where I needed to pass the Microsoft Data Access Application Blocks a dynamic connection string. Out of the box, the DAAB does not support this.
Solution: Create a simple method that accepts a connection string and return a Database object.
Code Before:Database db = DatabaseFactory.CreateDatabase();Code After:Database db = CustomDatabaseFactory.CreateDatabase(connstring);Class Code:1using System;
2using System.Collections.Generic;
3using System.Data.Common;
4using System.Text;
5using Microsoft.Practices.EnterpriseLibrary.Data;
6namespace Satisfyd.DataAccessLayer
7{
8//This class is used to dynamically set the connection string
9//while using the Microsoft Data Application Blocks
10publicstaticclass CustomDatabaseFactory
11 {
12staticreadonly DbProviderFactory dbProviderFactory =
13 DbProviderFactories.GetFactory("System.Data.SqlClient");
1415publicstatic Database CreateDatabase(string connectionString)
16 {
17returnnew GenericDatabase(connectionString, dbProviderFactory);
18 }
19 }
20}
Comments
|
On 1/8/2010
Sillion Nightfrost
said:
Thanks!
On 9/16/2008
Amit Prajapati
said:
Thanks, it's very usefull to me.
On 6/20/2008
Peter Marriott
said:
Thank you, works a treat!
|
Leave a Comment