Click here to Skip to main content
15,606,012 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
hi!! i need to implement backup and restore feature in my winform application...

currently i have set the connectionstring in my app.confg file and its working perfectly fine...
i am using Access 2007 for Database.

my app.confg code..

       <add key="connstr" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\items.accdb" />

my problem, is when the user selects a backup file.. the connectionstring path needs to be changed..

eg- suppose the user selects a backup file 'today_back.bak' now, the file name in app.confg should be changed..

    <add key="connstr" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\today_back.accdb" />

how can i do this can someone please help me... :)

Thanks in Advance
Kishor Deshpande 23-Feb-13 9:13am    
Have a separate key for these two databases named "back" and "items", and a static connection string property, on user selection of "Backup file", change your static connection string property to "back" key.
So it will now point to back up db.
Hope this helps.
Member 7849477 23-Feb-13 9:16am    
Thanks for the reply
can you give me an example..
Kishor Deshpande 23-Feb-13 9:17am    
That means I'll have to code, dnt worry I'll do it now :) and post it as answer.
Member 7849477 23-Feb-13 9:18am    
thanks :-)
Kishor Deshpande 23-Feb-13 11:03am    
Refer solution..

1 solution

Combo box has two values Live and Backup, on selected index change code is:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
           DataConnection.ConnectionString = ConfigurationManager.ConnectionStrings[comboBox1.SelectedItem.ToString()].ConnectionString;
           label1.Text = DataConnection.ConnectionString;

I'm just printing connection string on my label where as you can use it to communicate with DB as and when required as it is assigned to your DataConnection.ConnectionString property which holds ConnectionString.
Add System.Configuration namespace to use ConfigurationManager class.

My app.config file:
    <add name="Live" connectionstring="LiveDB" />
    <add name="Backup" connectionstring="BackupDB" />

Now my DataConnection class is the class which is communicating with DB based on ConnectionString property value.

class DataConnection
        public static string ConnectionString { get; set; }

Mark this as acceptable answer if it resolves your issue.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900