在实际项目里,我们需要用一个应用程序去连接多个数据库,有的进行测试,有的是数据库基本结构相同,数据不同, 我们不可能总去程序的连接字符串里去修改,更不能让用户去修改,所以需要动态去修改连接数据库配置信息。如果安全性可考虑的话需要对字符串加密,我这里写点简单的实现,希望大家有好的方法或意见,请执教和批评。

1 在应用程序里添加app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--   User application and configured property settings go here.-->
        <!--   Example: <add key="settingName" value="settingValue"/> -->
        <add key="ServerIP" value="127.0.0.1"/>
        <add key="Server" value="Automation_temp"></add>
        <add key="user" value="sa"></add>
        <add key="password" value="shan"></add>
</appSettings>
</configuration>

程序读取数据库连接,如下:

如果想把连接的信息显示出来,可以去解析字符串strcon,获取相关信息
private void Open()
        ...{
            // open connection
            if (con == null)
            ...{
                
                string strcon=String.Format ("packet size=4096;data source={0};persist security info=True;initial catalog={1};user id={2};password={3}",ConfigurationSettings.AppSettings["SQLserverIP"],
                   ConfigurationSettings.AppSettings["Server"],ConfigurationSettings.AppSettings["user"],ConfigurationSettings.AppSettings["password"]);
                con = new SqlConnection(strcon);
                try
                ...{
                    con.Open();
                }
                catch(Exception ee)
                ...{
                    ee.ToString();
                }
            }                
        }

2 新建窗体ConfigFrm

添加4个label  ,分别是:

服务器ip,Database Name,SA,password,

4个TextBox,分别是:

txtIP

txtDataBaseName

txtName

txtPwd

1个确认按钮btnOK,

3  写个方法保存修改的设置:

private void SaveConfig(string ConnenctionString,string strKey)
        ...{
            XmlDocument doc=new XmlDocument();
            //获得配置文件的全路径
            string strFileName=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes=doc.getElementsByTagName_r("add");
            for(int i=0;i<nodes.Count;i++)
            ...{
                //获得将当前元素的key属性
                XmlAttribute att=nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value==strKey)
                ...{
                    //对目标元素中的第二个属性赋值
                    att=nodes[i].Attributes["value"];
                    att.Value=ConnenctionString;
                    break;
                }
                
                
            }
            //保存上面的修改
            doc.Save(strFileName);
        }

4 在确认按钮btnOK click事件:

private void btnOK_Click(object sender, System.EventArgs e)
        ...{
            if (txtIP.Text=="")
            ...{
                MessageBox.Show("ServerIP is not allow null");
                return ;
            }
            else if(txtDataBaseName.Text=="")
            ...{
                MessageBox.Show("DataBase is not allow null");
                return ;
            }
            else if(txtName.Text=="")
            ...{
                MessageBox.Show("User Name is not allow null");
                return ;
            }
            else
            ...{
                SaveConfig(txtIP.Text,"ServerIP");
                SaveConfig(txtDataBaseName.Text,"Server");
                SaveConfig(txtName.Text,"user");
                SaveConfig(txtPassword.Text,"password");
                MessageBox.Show("Config Sucessful!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            this.Close();
            
        }

在应用程序当前目录下,程序动态加载的是 /bin/debug/test.exe.config信息,从而实现了动态读写xml文件,去获取

数据库连接。

在实际项目里,我们需要用一个应用程序去连接多个数据库,有的进行测试,有的是数据库基本结构相同,数据不同, 我们不可能总去程序的连接字符串里去修改,更不能让用户去修改,所以需要动态去修改连接数据库配置信息。如果安全性可考虑的话需要对字符串加密,我这里写点简单的实现,希望大家有好的方法或意见,请执教和批评。

1 在应用程序里添加app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--   User application and configured property settings go here.-->
        <!--   Example: <add key="settingName" value="settingValue"/> -->
        <add key="ServerIP" value="127.0.0.1"/>
        <add key="Server" value="Automation_temp"></add>
        <add key="user" value="sa"></add>
        <add key="password" value="shan"></add>
</appSettings>
</configuration>

程序读取数据库连接,如下:

如果想把连接的信息显示出来,可以去解析字符串strcon,获取相关信息
private void Open()
        ...{
            // open connection
            if (con == null)
            ...{
                
                string strcon=String.Format ("packet size=4096;data source={0};persist security info=True;initial catalog={1};user id={2};password={3}",ConfigurationSettings.AppSettings["SQLserverIP"],
                   ConfigurationSettings.AppSettings["Server"],ConfigurationSettings.AppSettings["user"],ConfigurationSettings.AppSettings["password"]);
                con = new SqlConnection(strcon);
                try
                ...{
                    con.Open();
                }
                catch(Exception ee)
                ...{
                    ee.ToString();
                }
            }                
        }

2 新建窗体ConfigFrm

添加4个label  ,分别是:

服务器ip,Database Name,SA,password,

4个TextBox,分别是:

txtIP

txtDataBaseName

txtName

txtPwd

1个确认按钮btnOK,

3  写个方法保存修改的设置:

private void SaveConfig(string ConnenctionString,string strKey)
        ...{
            XmlDocument doc=new XmlDocument();
            //获得配置文件的全路径
            string strFileName=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes=doc.getElementsByTagName_r("add");
            for(int i=0;i<nodes.Count;i++)
            ...{
                //获得将当前元素的key属性
                XmlAttribute att=nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value==strKey)
                ...{
                    //对目标元素中的第二个属性赋值
                    att=nodes[i].Attributes["value"];
                    att.Value=ConnenctionString;
                    break;
                }
                
                
            }
            //保存上面的修改
            doc.Save(strFileName);
        }

4 在确认按钮btnOK click事件:

private void btnOK_Click(object sender, System.EventArgs e)
        ...{
            if (txtIP.Text=="")
            ...{
                MessageBox.Show("ServerIP is not allow null");
                return ;
            }
            else if(txtDataBaseName.Text=="")
            ...{
                MessageBox.Show("DataBase is not allow null");
                return ;
            }
            else if(txtName.Text=="")
            ...{
                MessageBox.Show("User Name is not allow null");
                return ;
            }
            else
            ...{
                SaveConfig(txtIP.Text,"ServerIP");
                SaveConfig(txtDataBaseName.Text,"Server");
                SaveConfig(txtName.Text,"user");
                SaveConfig(txtPassword.Text,"password");
                MessageBox.Show("Config Sucessful!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            this.Close();
            
        }

在应用程序当前目录下,程序动态加载的是 /bin/debug/test.exe.config信息,从而实现了动态读写xml文件,去获取

数据库连接。

最新文章

  1. 计算机网络中的帧封装(C实现)
  2. LAMP简易安装
  3. iOS 判断设备型号
  4. [Irving]字符串相似度-字符编辑距离算法(c#实现)
  5. 恒天云技术分享系列6 – vLan网络原理解析
  6. JVM之字节码——Class文件格式
  7. ng-if与ng-show、ng-hide指令的区别和注意事项
  8. SCU 4440 Rectangle 2015年四川省赛题
  9. 浅谈SpringMVC(一)
  10. 编译在android 平台上跑的C应用程序
  11. java 流输出的一些问题
  12. python日期格式化操作
  13. 关于jQuery——attr方法和prop方法获取input的checked属性操作
  14. poj1182 食物链(并查集 好题)
  15. SpringAOP日志配置
  16. 面向对象特征:封装、多态 以及 @propetry装饰器
  17. C#-MVC开发微信应用(7)--在管理系统中同步微信用户分组信息
  18. Bugku-CTF之web2-听说聪明的人都能找到答案
  19. INNODB insert buffer 简单分析
  20. bzoj千题计划237:bzoj1492: [NOI2007]货币兑换Cash

热门文章

  1. win32线程
  2. 获取完全一样的数据库,包括表与表之间的外键关系,check,default表结构脚本
  3. 【转载】浅谈38K红外发射接受编码
  4. webservice的两种调用方式
  5. 第7章 Linux上配置RAID
  6. Perl的time、localtime和gmtime函数
  7. Redis持久化及其配置
  8. python安装Jieba中文分词组件并测试
  9. 《C#并发编程经典实例》学习笔记—异步编程关键字 Async和Await
  10. Python多线程的简单实现(生产者消费者模型)