Solved

c# Multiple Database - app.config

Posted on 2010-08-30
6
1,515 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 69

Expert Comment

by:Éric Moreau
ID: 33557421
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
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 use and create new code templates in NetBeans IDE 8.0 for Windows.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now