Data Connection Dialog (直接调用内置数据源连接对话框)

winform程序,在发布时往往需要更改数据库连接字符串,而数据库采用附加的方式配置数据库连接字符串,可以使用VS2005的配置界面来处理 

引用C:/Program Files/Microsoft Visual Studio 8/Common7/IDE 里面的Microsoft.Data.ConnectionUI.Dialog.dll。

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->using Microsoft.Data.ConnectionUI;
private void button1_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dia=new DataConnectionDialog ();
            dia.DataSources.Add(DataSource.SqlDataSource);
            dia.SelectedDataProvider = DataProvider.SqlDataProvider;
            if (DataConnectionDialog.Show(dia, this) == DialogResult.OK )
            {
                string myConnect = dia.ConnectionString;
                MessageBox.Show(myConnect);
             }
           
        }

Data Connection Dialog (直接调用内置数据源连接对话框)        }


为了保存数据库字符串,我们需要更改VS的配置文档(程序的connectString在配置文件内)。.exe文件的配置文件为.exe.config;在同一目录下

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->Data Connection Dialog (直接调用内置数据源连接对话框)XmlDocument myDoc = new XmlDocument();
Data Connection Dialog (直接调用内置数据源连接对话框)                XmlElement myXmlElement;
Data Connection Dialog (直接调用内置数据源连接对话框)                myDoc.Load(Application.ExecutablePath 
+ ".config");
Data Connection Dialog (直接调用内置数据源连接对话框)                XmlNode myNode 
= myDoc.SelectSingleNode("//connectionStrings");
Data Connection Dialog (直接调用内置数据源连接对话框)                myXmlElement 
= (XmlElement)myNode.SelectSingleNode("//add [@name='NXY.Properties.Settings.nxyInfoConnectionString']");
Data Connection Dialog (直接调用内置数据源连接对话框)                myXmlElement.SetAttribute(
"connectionString", myConnect);
Data Connection Dialog (直接调用内置数据源连接对话框)                myDoc.Save(Application.ExecutablePath 
+ ".config");


相对应的XML文件

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
    
</configSections>
    
<connectionStrings>
        
<add name="NXY.Properties.Settings.nxyInfoConnectionString" connectionString="Data Source=SC440;Initial Catalog=nxyInfo;Persist Security Info=True;User ID=sa;Password=sa"
            providerName
="System.Data.SqlClient" />
    
</connectionStrings>
</configuration>

本文转自yonghu86 51CTO博客,原文链接:http://blog.51cto.com/yonghu/1321398,如需转载请自行联系原作者
上一篇:《React Native 精解与实战》书籍连载「React Native 底层原理」


下一篇:MVC3缓存之一:使用页面缓存