How to dynamically create db connection and use in EntLib 5?

Topics: Data Access Application Block
Nov 4, 2010 at 4:14 PM
Edited Nov 4, 2010 at 4:17 PM

I'm using EntLib 5 and trying to build web application where various db owners will register their databases (MsSQL, Oracle,...) and application will retrieve data from them. I would like to use DatabaseFactory.CreateDatabase for connection, but I can't, because connections to databases are not predefined in web.config Is there any way how to dynamically (programmatically) create connection to database and connect to it using enterprise library? Thanks.

Nov 5, 2010 at 12:50 AM

The way around this would be to instantiate a specific database:

Database db = new SqlDatabase(connectionString);

or to build a configuration source containing the connection strings using the Fluent Configuration API and supplying that instance of IConfigurationSource in a DatabaseProviderFactory.

DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(configSource); 

Database db = dbFactory.Create("databaseInstanceName");


Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.

Nov 5, 2010 at 8:14 AM

It works. Thanks!