帶有對話方塊的資料庫配置源碼

來源:互聯網
上載者:User

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
using System.Xml;
namespace DSZQ
{
    public partial class DataConn : Form
    {
        public DataConn()
        {
            InitializeComponent();
            //filepath = Environment.CurrentDirectory;
            filepath =Application.StartupPath;
            filepath += "//App.config";
        }
        string filepath;
        string connstr;
       /// <summary>
       ///
       /// </summary>
       /// <param name="key"></param>
       /// <param name="strValue"></param>
        public void Modify(string key, string strValue)          //兩個參數:要修改的索引值   和   要修改的新值;                              

        {
          string   flagstr = strValue;
            if (strValue == string.Empty)
            {
                MessageBox.Show("串連串不可為空!");
                return;
            }
            //string XPath = "/configuration/userInfo/add[@key='?']";
            try
            {
                string XPath = "/configuration/appSettings/add[@key='?']";
                XmlDocument domWebConfig = new XmlDocument();

                //domWebConfig.Load((HttpContext.Current.Server.MapPath("web.config")));
                domWebConfig.Load(filepath);
                XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));

                if (addKey == null)
                {
                    //Response.Write("<script>alert   (\"沒有找到<add   key='" + key + "'   value=.../>的配置節\")</script>");
                    MessageBox.Show("沒有找到<add   key='" + key + "'>的配置節");
                    return;
                }
                addKey.Attributes["value"].InnerText = strValue;
                domWebConfig.Save(filepath);
                MessageBox.Show("資料庫連接配置成功","資訊提示");
                txtConnectionString.Text = connstr;
                txtConnectionString.Enabled = false;
            }
            catch
            {
               // MessageBox.Show("在"+Environment.CurrentDirectory + "目錄下找不到web.config設定檔","資訊提示");
                txtConnectionString.Enabled = true;
                txtConnectionString.Clear();
                txtConnectionString.ForeColor = Color.Red;
                txtConnectionString.Text ="在" + Application.StartupPath + "目錄下找不到App.config設定檔";
                return;
            }

        }

        private void buttonConn_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dialog = new DataConnectionDialog();
            //添加資料來源列表,可以向視窗中添加自己程式所需要的資料來源類型
            dialog.DataSources.Add(DataSource.SqlDataSource);
            dialog.DataSources.Add(DataSource.OdbcDataSource);

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

            //只能夠通過DataConnectionDialog類的靜態方法Show出對話方塊
            //不同使用dialog.Show()或dialog.ShowDialog()來呈現對話方塊
            if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK)
            {
                connstr = dialog.ConnectionString;
                connstr = "Provider=SQLOLEDB.1;" + connstr;
                Modify("ConnStr",connstr);
             

            }
        }

   
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.