Click here to Skip to main content
15,886,110 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..

XML
<appSettings>
       <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..


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



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

Thanks in Advance
Posted
Comments
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.
Thanks.
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:
C#
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:
HTML
<configuration>
  <connectionstrings>
    <add name="Live" connectionstring="LiveDB" />
    <add name="Backup" connectionstring="BackupDB" />
  </connectionstrings>
</configuration>


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

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


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

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