Solved

c# Multiple Database - app.config

Posted on 2010-08-30
6
1,531 Views
Last Modified: 2013-12-17
Hi There,

Hope somebody would be kind enough to help me? Been searching the Net quite a while n ow but relly cant find anything similar?

Im working on a C# App (Newbie) where we now need to be able to specify more than 1 database to log on to.

Current the App is setup like:

connection_string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\RoleBasedControl\\RoleBasedControl\\Database\\LiveDB.accdb;Persist Security Info=True";
con = new OleDbConnection(connection_string);
con.Open();

and so one ....

I have now added a ComboxBox called cboSelectDB

I need to know how to populate the ComboxBox with the verious connections (LiveDB, StagingDB, TestDB,) in ConnectionStrings.Config.
I also and need to know how to update the connection_string = in the app to load the database which is selected in the ComboBox.

App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings configSource ="Xml/ConnectionStrings.config"/>
</configuration>

ConnectionStrings.Config:

<?xml version="1.0" encoding="utf-8" ?>
  <connectionStrings>
    <add name="Live Database" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\RoleBasedControl\RoleBasedControl\Database\LiveDB.accdb;Persist Security Info=True" />
    <add name="Test Database" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\RoleBasedControl\RoleBasedControl\Database\TestDB.accdb;Persist Security Info=True" />
    <add name="Staging Database" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\RoleBasedControl\RoleBasedControl\Database\StagingDB.accdb;Persist Security Info=True" />
  </connectionStrings>
 
 Cheers
 Si
0
Comment
Question by:SimonGreen2004
6 Comments
 

Expert Comment

by:gayan007
ID: 33557095
you don't need a separate ConnectionStrings.Config: file since you can specify all the connection strings on app.config file.
you can create and open any number of database connections as you have shown.
You just have to use correct DB to save and retrieve data.
0
 
LVL 1

Author Comment

by:SimonGreen2004
ID: 33557172
Hi There,

The problem i have is there could be multiple databases for multiple users.

i.e - Each Developer will have a seperate DB. Some will have DB's clled test, some clled Tester, etc.

Everyone generally will have different DB's. I dont want the App.Config been changed as it has global requirements in. Different Users can have different ConnectionStrings.Config, but the same *Global* app.config.

Hopefully that makes a bit more sense:)

Cheers
Si
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33557421
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33557422
Hi,

Please rectify me if I am wrong in understanding your problem.
So here is the revised problem statement of yours:
You are having a Server on your system with number of database(s) in it. Right? And you also don't want to change your App.config (Global) file. And you want that if user selects connectionstring from database list in combobox or you can try to store different databases in your combobox.

And my Solution for that:
In that case I would recommend you to store database name in combobox. And then create your connectionstring dynamically at runtime by getting value from combobox selected by user.

Hope it helps you to develop code. Write back for more assistance.

Regards,
VSS
0
 
LVL 1

Author Comment

by:SimonGreen2004
ID: 33557749
Hi vs00saini,

You are correct. Appologies i think i may have confused everyone in my opening question.

Im actually after some code snippets for how i would do the above, based on the code i have already posted.

Cheers
Si
0
 
LVL 1

Accepted Solution

by:
JensMig earned 250 total points
ID: 33562688
It's much simpler than that.

Add System.Configuration as reference.
Then you have access to all connection strings and their names in your config.
System.Configuration.ConfigurationManager.ConnectionStrings offers a static list of connectionstring settings, where name and connection string itself are properties.

You could use this to populate your combobox, even with names that speak for themselves :-)


foreach (System.Configuration.ConnectionStringSettings setting in System.Configuration.ConfigurationManager.ConnectionStrings)
{
    string name = setting.Name;
    string connString = setting.ConnectionString;
}

Open in new window

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question