Click here to Skip to main content
11,797,944 members (78,452 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Access .NET4
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... Smile | :)

Thanks in Advance
Posted 23-Feb-13 3:08am
Kishor Deshpande at 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 at 23-Feb-13 9:16am
Thanks for the reply
can you give me an example..
Kishor Deshpande at 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 at 23-Feb-13 9:18am
thanks :-)
Kishor Deshpande at 23-Feb-13 11:03am
Refer solution..
savbace at 23-Feb-13 9:47am
Take a look here:

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
0 CPallini 575
1 Maciej Los 375
2 OriginalGriff 353
3 Abhinav S 342
4 Richard MacCutchan 218
0 OriginalGriff 2,150
1 Maciej Los 1,790
2 KrunalRohit 1,456
3 CPallini 1,415
4 Richard MacCutchan 1,016

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100