Solved

c# Multiple Database - app.config

Posted on 2010-08-30
6
1,535 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
Technology Partners: 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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

733 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