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

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

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

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

    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);
             }          
       } 

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

XmlDocument myDoc = new XmlDocument();
XmlElement myXmlElement;
myDoc.Load(Application.ExecutablePath + ".config");
XmlNode myNode = myDoc.SelectSingleNode("//connectionStrings");
myXmlElement = (XmlElement)myNode.SelectSingleNode("//add [@name='NXY.Properties.Settings.nxyInfoConnectionString']");
myXmlElement.SetAttribute("connectionString", myConnect);
myDoc.Save(Application.ExecutablePath + ".config"); 

  相对应的XML文件 

<?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>

 

posted @ 2010-12-20 23:13  .NET快速开发框架  阅读(1858)  评论(0编辑  收藏  举报