コード例 #1
0
        /// <summary>
        /// 保存数据源连接地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveDB_Click(object sender, EventArgs e)
        {
            if (this.tbDataSource.Text == "")
            {
                MessageBox.Show("数据源不能为空!", "操作提示", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
            if (this.tbDatabase.Text == "")
            {
                MessageBox.Show("数据库不能为空!", "操作提示", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
            if (this.tbUserName.Text == "")
            {
                MessageBox.Show("数据库用户名不能为空!", "操作提示", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
            if (this.tbPasswd.Text == "")
            {
                MessageBox.Show("数据库密码不能为空!", "操作提示", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }

            try
            {
                string filepath           = Application.ExecutablePath + ".config";
                string conString          = "Data Source=" + this.tbDataSource.Text + ";User ID=" + this.tbUserName.Text + ";Password="******";Initial Catalog=" + this.tbDatabase.Text;
                string autoDataBaseString = "Data Source=" + this.tbDataSource.Text + ";User ID=" + this.tbUserName.Text + ";Password="******";Initial Catalog=master";


                bool boolCon = LinQBaseDao.DetectionConn(conString);
                if (!boolCon)
                {
                    MessageBox.Show("此数据源配置无法连接,重新输入正确的数据源配置!", "操作提示", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }

                XmlDocument doc = new XmlDocument();
                doc.Load(filepath);
                XmlNodeList node = doc.SelectSingleNode("configuration").ChildNodes;
                if (node.Count > 0)
                {
                    XmlElement  ele      = (XmlElement)node[1];
                    XmlNodeList nodeList = ele.ChildNodes;
                    if (nodeList.Count > 0)
                    {
                        XmlElement ele2 = (XmlElement)nodeList[0];
                        ele2.SetAttribute("connectionString", conString);
                        doc.Save(filepath);
                    }
                }
                btWriteReg(conString);
                //btWriteReg2(autoDataBaseString);
                MessageBox.Show("修改成功!请退出系统,重新启动程序!", "操作提示", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                //System.Environment.Exit(System.Environment.ExitCode);
                //Application.ExitThread();
                Common.WriteLogData("修改", "数据源配置", Common.NAME); //操作日志
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改数据配置失败!", "操作提示", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }
コード例 #2
0
        /// <summary>
        /// “数据源配置” 按钮的“保 存”单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveDB_Click(object sender, EventArgs e)
        {
            if (this.txtDataSource.Text == "")
            {
                MessageBox.Show("数据源不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (this.txtDatabase.Text == "")
            {
                MessageBox.Show("数据库不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (this.txtUserName.Text == "")
            {
                MessageBox.Show("数据库用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (this.txtPwd.Text == "")
            {
                MessageBox.Show("数据库密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                string filepath           = Application.ExecutablePath + ".config";
                string conString          = "Data Source=" + this.txtDataSource.Text.Trim() + ";User ID=" + this.txtUserName.Text.Trim() + ";Password="******";Initial Catalog=" + this.txtDatabase.Text.Trim();
                string autoDataBaseString = "Data Source=" + this.txtDataSource.Text.Trim() + ";User ID=" + this.txtUserName.Text.Trim() + ";Password="******";Initial Catalog=master";
                bool   boolCon            = LinQBaseDao.DetectionConn(conString);
                if (!boolCon)
                {
                    MessageBox.Show("此数据源配置无法连接,重新输入正确的数据源配置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                XmlDocument doc = new XmlDocument();
                doc.Load(filepath);
                XmlNodeList node = doc.SelectSingleNode("configuration").ChildNodes;
                if (node.Count > 0)
                {
                    // XmlNodeList dsf = doc.SelectSingleNode("connectionStrings").ChildNodes;
                    XmlElement  ele      = (XmlElement)node[1];
                    XmlNodeList nodeList = ele.ChildNodes;
                    if (nodeList.Count > 0)
                    {
                        XmlElement ele2 = (XmlElement)node[1];

                        XmlNodeList xnl = ele2.ChildNodes;
                        if (xnl.Count > 0)
                        {
                            XmlElement xe = (XmlElement)xnl[0];
                            xe.SetAttribute("connectionString", conString);
                            doc.Save(filepath);

                            btWriteReg(conString); // 调用写入连接数据源到 XML 的方法
                            if (MessageBox.Show("数据源修改成功!是否退出系统重新启动程序?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                            {
                                System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                                Application.Restart();
                            }
                            CommonalityEntity.WriteLogData("修改", "数据源配置", common.NAME); //操作日志
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("修改数据配置失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }