Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Access .NET4 WinForm
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..
 
<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..

 
<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... Smile | :)
 
Thanks in Advance
Posted 23-Feb-13 4:08am
Comments
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.
Thanks.
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: http://stackoverflow.com/questions/63546/vs2005-c-sharp-programmatically-change-connection-string-contained-in-app-config
Regards!

1 solution

Rate this: bad
good
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:
 
<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.
 
class DataConnection
    {
        public static string ConnectionString { get; set; }
    }
 
Mark this as acceptable answer if it resolves your issue.
Thanks.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2014
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