SqlConfigurationSource and DatabaseFactory.CreateDatabase(client)

Topics: Data Access Application Block, Enterprise Library Core, General discussion
Jul 20, 2011 at 8:13 AM


I came across a discussion in the codeplex on using the SqlConfigurationSource for storing the ConnectionStrings.

Currently I have so many clients for my application and all these client connectionstrings are maintained in the web.config. Now I wanted to move the connection strings to the DB using SqlConfigurationSource.

My requirement is, currently we are calling the DatabaseFactory.CreateDatabase(client) where client is a connectionstring name in web.config. What I would like to do is store the connection string info for the client in the DB and let the DatabaseFactory.CreateDatabase(client) pick the matching connection string from the DB and use it accordingly to create the DB.

I haven’t tried using the SqlConfigurationSource and need help on how to use that according to my situation like.

Can you please guide me on the below points

1. How to use the SqlConfigurationSource in the web.config?

2. What are the steps required to store the info in the DB?

 3. Does the DatabaseFactory.CreateDatabase(client) automatically picks the config information from the DB?

4. What are the pre-requisites if any?

5. Does the connection string values can be encrypted in the DB or these care plain text? How can we encrypt them?

6. I have some other connectionstrings also defined in the web.config like membershipprovider connection string, so can we keep only some connectionstring(client connection string) in the DB and some connectionstrings(membsership and globalDB) in the config file?

7. Can you give me details on how to use the SqlConfigurationSource step by step?

8. The SqlConfigurationSource is not showing in the ent lib config tool. What needs to be done?

Thanks in advance and thanks for sharing the information.

-Pavan Kumar R

Jul 20, 2011 at 1:10 PM
Hi Pavan,

Here are my answers to your questions:

1) You can simply do that with the config editor. Go to Blocks > Add Configuration Settings. Then on Sources, click the "+" then Add Sources > Add Sql Server Configuration Source. Then add the redirected sections like shown below:

Sql Configuration Source

2) Create an instance of SqlConfigurationSource then call the Add method. Here is an example snippet:

         SqlConfigurationSource source = new SqlConfigurationSource(new SqlConfigurationDb([connection string to the database]));
         source.Add([section name], [the actual configuration section]);

3) Yes, it should pick up the config settings given that you've set up your sql config source properly.

4) Of course you need to have a reference to Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.dll and also have a connection with your database.

5) Its not encrypted by default. You must do the encryption yourself.

6) Im not sure with this one but according to the ReadMe file:

"The <connectionStrings> section is treated specially. The contents of <connectionStrings> will be stored both in the configuration file AND in the database. If the two sections differ, Enterprise Library Data Access Block usage will use the settings from the database, while other data access will be pulling connection strings from the file."

7) Pretty much the same with #1 and #2 :)

8) Try to put the Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.dll to C:\Program Files\Microsoft Enterprise Library 5.0\Bin

Hope I answered all your questions :)


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us