Solved

c# Multiple Database - app.config

Posted on 2010-08-30
6
1,541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

705 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