使用Visual Studio的動態連線庫建立通用資料庫連線對話方塊

衣舞晨風發表於2015-08-07

1、在VS(此處目錄檔案以vs2010為例)安裝目錄下(“%Visual Studio安裝目錄%/Common10/IDE/Microsoft.Data.ConnectionUI.Dialog.dll”)找到Microsoft.Data.ConnectionUI.dll、Microsoft.Data.ConnectionUI.Dialog.dll和漢化資原始檔Microsoft.Data.ConnectionUI.Dialog.resources.dll,注意漢化資原始檔需要放在zh-CHS資料夾下;



2、在工程檔案中新增對以上二個dll檔案的引用;

3、具體程式碼如下:

 private void button1_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dialog = new DataConnectionDialog();
            dialog.DataSources.Add(DataSource.AccessDataSource);
            dialog.DataSources.Add(DataSource.OdbcDataSource);
            dialog.DataSources.Add(DataSource.OracleDataSource);
            dialog.DataSources.Add(DataSource.SqlDataSource);
            dialog.DataSources.Add(DataSource.SqlFileDataSource);

            dialog.SelectedDataSource = DataSource.SqlDataSource;
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;

            if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK)
            {
                //connDlg.ConnectionString;
            }
        }
執行效果:


重新開啟時展示,之前選擇的連線字串:

 DataConnectionDialog dialog = new DataConnectionDialog();        
            dialog.DataSources.Add(DataSource.AccessDataSource);
            dialog.DataSources.Add(DataSource.OdbcDataSource);
            dialog.DataSources.Add(DataSource.OracleDataSource);
            dialog.DataSources.Add(DataSource.SqlDataSource);
            dialog.DataSources.Add(DataSource.SqlFileDataSource);

            dialog.SelectedDataSource = DataSource.SqlDataSource;
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
            //之前 DataConnectionDialog 控制元件儲存的連線字串
            dialog.ConnectionString = str;
            //ConnectionString有值,則會顯示出來
            DataConnectionDialog.Show(dialog, this);

相關文章