当用户选择备份文件时如何更改连接字符串路径... [英] how to change the connectionstring path when the user selects a backup file...

查看:72
本文介绍了当用户选择备份文件时如何更改连接字符串路径...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜!!我需要在我的winform应用程序中实现备份和恢复功能...



目前我已经在app.confg文件中设置了connectionstring,并且它工作得很好.. 。

i我正在使用Access 2007 for Database。



我的app.confg代码..



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









我的问题是,当用户选择备份文件时......需要更改连接字符串路径..



例如 - 假设用户选择了备份文件''today_back.bak''现在,应该更改app.confg中的文件名..





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







我怎么能这样做有人请帮助我...... :)



先谢谢

解决方案

组合框有两个值Live和Backup,在选定的索引更改代码上是:

  private   void  comboBox1_SelectedIndexChanged( object  sender,EventArgs e)
{
DataConnection.ConnectionString = ConfigurationManager.ConnectionStrings [comboBox1.SelectedItem.ToString()]。ConnectionString;
label1.Text = DataConnection.ConnectionString;
}



我只是在我的标签上打印连接字符串,因为您可以使用它来与DB通信,并在需要时它被分配给包含ConnectionString的DataConnection.ConnectionString属性。

添加System.Configuration命名空间以使用ConfigurationManager类。



我的应用程序。配置文件:

 <  配置 >  
< connectionstrings >
< add name = 实时 connectionstring = LiveDB / >
< < span class =code-leadattribute> add
name = 备份 connectionstring = BackupDB / >
< / connectionstrings >
< / configuration >




现在我的DataConnection类是基于ConnectionString属性值与DB通信的类。



< pre lang =c#> class DataConnection
{
public static string ConnectionString { get ; set ; }
}





如果能解决您的问题,请将此标记为可接受的答案。

谢谢。


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... :)

Thanks in Advance

解决方案

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.


这篇关于当用户选择备份文件时如何更改连接字符串路径...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆