Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# Multiple Database - app.config

Posted on 2010-08-30
6
Medium Priority
?
1,585 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

972 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