Add new database connection from my application

Topics: Data Access Application Block
Oct 1, 2008 at 7:00 PM
Hello to everyone.

Sometime now i am studying the Data Access Application Block. Although it is simple to understand i came across one requirement that so far i cannot solve.

How am i suppose to create many connection strings, and delete them if i want, not using the Enterprise Library Configuration tool, but from a form that i have already created for this reason. So that from my customer's view he can connect to many databases, unknown to me and manage his connections.  Of course the application needs to write back to app.config what the user entered, so it can remember at next start and, also delete what the user deleted. Is there any project that i can look, that takes care of this? Anyone needed that?

Thank you.
Oct 2, 2008 at 1:41 AM
The connection string can be stored in the application configuration file, or it can be stored externally. The following is from the Ent Lib 4.0 help file. 

Use default connection string
Database dbSvc = DatabaseFactory.CreateDatabase();
Named config entry:
// Use a named database instance that refers to an arbitrary database type, 
// which is determined by configuration information.
Database myDb = DatabaseFactory.CreateDatabase("Sales");
External connection string:
// Assume your application contains the routine GetConnectionString. 
string myConnectionString = GetConnectionString();

SqlDatabase sqlDatabase = new SqlDatabase(myConnectionString);

Your question is not really an Enterprise Library question, more a dynamic, user configurable connection string question.  Other idea is to store the connection string in the user or local machine hive of the registry.
Oct 2, 2008 at 1:00 PM
Hi and thank you for your reply.

Actually you give me a good chance to continue my post and express my ideas. DAAB (Data Access Application Block) takes care in a unified way your communication with your database no matter what database is that. I have already implemented the solution that you describe but at the end i came at the same line of code that you wrote earlier.

SqlDatabase sqlDatabase = new SqlDatabase(myConnectionString); What we see here is explicitly declare the type of database we want to have. We don't use anymore the beauty of  Database dbSvc = DatabaseFactory.CreateDatabase or Database myDB = DatabaseFactory.CreateDatabase("Sales"). As you can see no explicitly declaration of the database type.

When you create an application you can use the Enterprise Library Configuration to handle your connections (not one many connections) but that requires what? To know already your connections. If you don't? If you don't know what kind of database the end customer will use how you do that? You cannot give to each of your customers a Enterprise Library Configurator and expect from them to do it. So from my point of you i would expect not from the Enterprise Library i was specific about that, but from the DAAB to give you few routines that you can handle your connection in your app.config. Routines like AddNewConnection, RemoveConnection, ConnectionsList, MakeConnectionDefault etc. I think that is functionality should be implemented in DAAB. And there are not out there applications that only connect to one single database type.

Well does DAAB support this? If not how you solve the dynamic database connection strings handling with multiple databases using DAAB? For me to declare the database type as above (SqlDatabase sqlDatabase = new SqlDatabase(myConnectionString);)  is so ugly first and second i don't even know if it is a SQL Server database and you know what? I don't even want to know.... That is the beauty of DAAB.
Oct 2, 2008 at 11:17 PM
Hi again.

After many hours googling i came up with two good solutions to my problem.

The first one takes advantage of the new System.Configuration improvements. The new ConfigurationManager class gives you back a collection of AppSettings, ConnectionStrings or any other section you declared in your app.config.

The second one is from Enterprise Library. Under EnterpriseLinrary.Common.Configuration you have also options to modify your app.configs files. Although under cover it uses the newly System.Configuration i don't see any point of using this but maybe there are situations that i should. I am new to this so reading must go on...

I have posted this just in case anyone was having the same questions as me.