Determining Valid Database Instance Names

Topics: Data Access Application Block
Apr 8, 2009 at 8:35 PM
Hello,

I am new to using EntLib 4.1 - using to 2.0.  In the building of our new application, it has been determined that there will be a number of different databases in use.  In the base class that I am writing, I want to insure that the database name that is being passed in is a valid one.  Is there a way to accomplish this from within the DAAB?  Or should I build in a different means of determining the database names, i.e. perhaps an enum?

Thanks,

David
Apr 13, 2009 at 11:21 AM
Edited Apr 13, 2009 at 11:21 AM
Hi,

To what extent you want to know that your database instance name is valid? Is it to the point that you ensure that it exists? AFAIK, there is no direct way from DAAB to enumerate all the database instance from the config. But you can try it this way. 

foreach (ConnectionStringSettings item in System.Configuration.ConfigurationManager.ConnectionStrings)
{
    item.Name;
}


this will enumerate all the database instance from the config file. Also, if you want to test if the database exist, try opening a connection against a database instance:

Database db = DatabaseFactory.CreateDatabase("DBINSTANCENAME");
DbConnection connection = db.CreateConnection();
connection.Open();

 

execute this in a try-catch block. This will throw a exception if the database that your trying to open a connection does not exist.

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com