Example #1
0
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            try
            {
                string server = this.comboBoxServer.Text.Trim();
                string user   = this.txtUser.Text.Trim();
                string pass   = this.txtPass.Text.Trim();
                string port   = this.textBox1.Text.Trim();
                if (user == "" || server == "")
                {
                    MessageBox.Show(this, "服务器或用户名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (this.cmbDBlist.SelectedIndex > 0)
                    {
                        dbname = cmbDBlist.Text;
                    }
                    else
                    {
                        dbname = "mysql";
                    }
                    constr = String.Format("server={0};user id={1}; Port={2};password={3}; database={4}; pooling=false", server, user, port, pass, dbname);
                    //测试连接
                    MySqlConnection myCn = new MySqlConnection(constr);
                    try
                    {
                        this.Text = "正在连接服务器,请稍候...";
                        myCn.Open();
                    }
                    catch (System.Exception ex)
                    {
                        LogInfo.WriteLog(ex);
                        this.Text = "连接服务器失败!";
                        MessageBox.Show(this, "连接服务器失败!请检查服务器地址或用户名密码是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }
                    finally
                    {
                        myCn.Close();
                    }
                    this.Text = "连接服务器成功!";
                    if (dbobj == null)
                    {
                        dbobj = new Maticsoft.CmConfig.DbSettings();
                    }
                    string strtype = "MySQL";
                    //将当前配置写入配置文件
                    dbobj.DbType            = strtype;
                    dbobj.Server            = server;
                    dbobj.ConnectStr        = constr;
                    dbobj.DbName            = dbname;
                    this.dbobj.DbHelperName = "DbHelperMySQL";
                    dbobj.ConnectSimple     = chk_Simple.Checked;
                    switch (DbConfig.AddSettings(this.dbobj))
                    {
                    case 0:
                        MessageBox.Show(this, "添加服务器配置失败,请检查安装目录是否有写入权限或文件是否存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;

                    case 2:
                    {
                        DialogResult dialogResult = MessageBox.Show(this, "该服务器信息已经存在!你确认是否覆盖当前数据库配置?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                        if (dialogResult != DialogResult.Yes)
                        {
                            return;
                        }
                        DbConfig.DelSetting(this.dbobj.DbType, this.dbobj.Server, this.dbobj.DbName);
                        int num = DbConfig.AddSettings(this.dbobj);
                        if (num != 1)
                        {
                            MessageBox.Show(this, "建议卸载当前版本,并删除安装目录后重新安装最新版本!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            return;
                        }
                        break;
                    }
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                LogInfo.WriteLog(ex);
            }
        }
Example #2
0
        private void BtnOk_Click(object sender, System.EventArgs e)
        {
            //try
            //{
            //    string user = this.txtUser.Text.Trim();
            //    string pass = this.txtPass.Text.Trim();
            //    string server = this.txtServer.Text.Trim();

            //    if ((user.Trim() == "") || (server.Trim() == ""))
            //    {
            //        MessageBox.Show(this, "用户名或密码不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }

            //    constr = "Data Source=" + server + "; user id=" + user + ";password="******"正在连接服务器,请稍候...";
            //        myCn.Open();
            //    }
            //    catch
            //    {
            //        this.Text = "连接服务器失败!";
            //        myCn.Close();
            //        MessageBox.Show(this, "连接服务器失败!请检查服务器地址或用户名密码是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        return;
            //    }
            //    myCn.Close();
            //    this.Text = "连接服务器成功!";


            //    if (dbobj == null)
            //        dbobj = new Maticsoft.CmConfig.DbSettings();

            //    //将当前配置写入配置文件
            //    dbobj.DbType = "Oracle";
            //    dbobj.Server = server;
            //    dbobj.ConnectStr = constr;
            //    dbobj.DbName = "";
            //    dbobj.ConnectSimple = chk_Simple.Checked;
            //    bool succ = Maticsoft.CmConfig.DbConfig.AddSettings(dbobj);
            //    if (!succ)
            //    {
            //        MessageBox.Show(this, "该服务器已经存在!请更换服务器地址或检查输入是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        return;
            //    }

            //    //将当前数据库系统类型写入配置
            //    //MainForm.setting.DbType="Oracle";
            //    //Maticsoft.CmConfig.ModuleConfig.SaveSettings(MainForm.setting);
            //    this.DialogResult = DialogResult.OK;
            //    this.Close();

            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    LogInfo.WriteLog(ex);
            //}
            try
            {
                if (this.chkboxConnectStr.Checked)
                {
                    if (this.txtConnectStr.Text.Length == 0)
                    {
                        MessageBox.Show(this, "连接字符串不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return;
                    }
                    this.constr = this.txtConnectStr.Text;
                }
                else
                {
                    string text  = this.txtUser.Text.Trim();
                    string text2 = this.txtPass.Text.Trim();
                    string text3 = this.txtServer.Text.Trim();
                    if (text.Trim() == "" || text3.Trim() == "")
                    {
                        MessageBox.Show(this, "用户名或密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return;
                    }
                    this.constr = string.Concat(new string[]
                    {
                        "Data Source=",
                        text3,
                        "; user id=",
                        text,
                        ";password="******"正在连接服务器,请稍候...";
                        oracleConnection.Open();
                    }
                    catch (Exception ex)
                    {
                        this.Text = "连接服务器失败!";
                        oracleConnection.Close();
                        LogInfo.WriteLog(ex);
                        MessageBox.Show(this, "连接服务器失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }
                    oracleConnection.Close();
                    this.Text = "连接服务器成功!";
                    if (this.dbobj == null)
                    {
                        this.dbobj = new DbSettings();
                    }
                    this.dbobj.DbType        = "Oracle";
                    this.dbobj.Server        = this.txtServer.Text.Trim();
                    this.dbobj.ConnectStr    = this.constr;
                    this.dbobj.DbName        = "";
                    this.dbobj.DbHelperName  = "DbHelperOra";
                    this.dbobj.ConnectSimple = this.chk_Simple.Checked;
                    switch (DbConfig.AddSettings(this.dbobj))
                    {
                    case 0:
                        MessageBox.Show(this, "添加服务器配置失败,请检查是否有写入权限或文件是否存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;

                    case 2:
                    {
                        DialogResult dialogResult = MessageBox.Show(this, "该服务器信息已经存在!你确认是否覆盖当前数据库配置?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                        if (dialogResult != DialogResult.Yes)
                        {
                            return;
                        }
                        DbConfig.DelSetting(this.dbobj.DbType, this.dbobj.Server, this.dbobj.DbName);
                        int num = DbConfig.AddSettings(this.dbobj);
                        if (num != 1)
                        {
                            MessageBox.Show(this, "建议卸载当前版本,并删除安装目录后重新安装最新版本!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            return;
                        }
                        break;
                    }
                    }
                    base.DialogResult = DialogResult.OK;
                    base.Close();
                }
            }
            catch (Exception ex2)
            {
                MessageBox.Show(this, ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                LogInfo.WriteLog(ex2);
            }
        }
Example #3
0
        private void btn_Ok_Click(object sender, System.EventArgs e)
        {
            try
            {
                string text  = this.comboBoxServer.Text.Trim();
                string text2 = this.txtUser.Text.Trim();
                string text3 = this.txtPass.Text.Trim();
                if (text2 == "" || text == "")
                {
                    MessageBox.Show(this, "服务器或用户名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    if (this.cmbDBlist.SelectedIndex > 0)
                    {
                        this.dbname = this.cmbDBlist.Text;
                    }
                    else
                    {
                        this.dbname = "master";
                    }
                    if (this.GetSelVerified() == "Windows")
                    {
                        this.constr = "Integrated Security=SSPI;Data Source=" + text + ";Initial Catalog=" + this.dbname;
                    }
                    else
                    {
                        if (text3 == "")
                        {
                            this.constr = string.Concat(new string[]
                            {
                                "User Id=",
                                text2,
                                ";Database=",
                                this.dbname,
                                ";Server=",
                                text
                            });
                        }
                        else
                        {
                            this.constr = string.Concat(new string[]
                            {
                                "User Id=",
                                text2,
                                ";Password="******";Database=",
                                this.dbname,
                                ";Server=",
                                text
                            });
                        }
                    }
                    string selVer = this.GetSelVer();
                    try
                    {
                        string sQLVer = this.GetSQLVer(this.constr);
                        if (sQLVer != "11" && selVer == "SQL2012")
                        {
                            DialogResult dialogResult = MessageBox.Show(this, "该数据库服务器版本并非SQLServer 2008,是否进行重新选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                            if (dialogResult == DialogResult.OK)
                            {
                                this.SelectServerVer(sQLVer);
                                return;
                            }
                        }
                        if (sQLVer != "10" && selVer == "SQL2008")
                        {
                            DialogResult dialogResult2 = MessageBox.Show(this, "该数据库服务器版本并非SQLServer 2008,是否进行重新选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                            if (dialogResult2 == DialogResult.OK)
                            {
                                this.SelectServerVer(sQLVer);
                                return;
                            }
                        }
                        if (sQLVer != "9" && selVer == "SQL2005")
                        {
                            DialogResult dialogResult3 = MessageBox.Show(this, "该数据库服务器版本并非SQLServer 2005,是否进行重新选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                            if (dialogResult3 == DialogResult.OK)
                            {
                                this.SelectServerVer(sQLVer);
                                return;
                            }
                        }
                        if (sQLVer != "8" && selVer == "SQL2000")
                        {
                            DialogResult dialogResult4 = MessageBox.Show(this, "该数据库服务器版本并非SQLServer 2000,是否进行重新选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                            if (dialogResult4 == DialogResult.OK)
                            {
                                this.SelectServerVer(sQLVer);
                                return;
                            }
                        }
                    }
                    catch
                    {
                    }
                    SqlConnection sqlConnection = new SqlConnection(this.constr);
                    try
                    {
                        this.Text = "正在连接服务器,请稍候...";
                        sqlConnection.Open();
                    }
                    catch (Exception ex)
                    {
                        this.Text = "连接服务器失败!";
                        LogInfo.WriteLog(ex);
                        string text4 = "连接服务器或获取数据信息失败!\r\n";
                        text4 += "1.请检查服务器地址或用户名密码是否正确!\r\n";
                        text4 += "2.请确保是SQLServer正式版,而非SQLEXPRESS版!\r\n";
                        text4 += "3.如果连接失败,服务器名可以尝试用“机器名”代替IP,或者 “(local)”或是“.”试一下!\r\n";
                        text4 += "4.如果需要查看帮助文件以帮助您解决问题,请点“确定”,否则点“取消”";
                        MessageBox.Show(this, text4, "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }
                    finally
                    {
                        sqlConnection.Close();
                    }
                    this.Text = "连接服务器成功!";
                    if (this.dbobj == null)
                    {
                        this.dbobj = new DbSettings();
                    }
                    this.dbobj.DbType        = selVer;
                    this.dbobj.Server        = text;
                    this.dbobj.ConnectStr    = this.constr;
                    this.dbobj.DbName        = this.dbname;
                    this.dbobj.DbHelperName  = "DbHelperSQL";
                    this.dbobj.ConnectSimple = this.chk_Simple.Checked;
                    if (this.cmboxTabLoadtype.SelectedIndex > 0)
                    {
                        if (this.txtTabLoadKeyword.Text.Trim().Length == 0)
                        {
                            MessageBox.Show(this, "请输入表名过滤的关键字!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            return;
                        }
                        this.dbobj.TabLoadtype    = this.cmboxTabLoadtype.SelectedIndex;
                        this.dbobj.TabLoadKeyword = this.txtTabLoadKeyword.Text.Trim();
                    }
                    switch (DbConfig.AddSettings(this.dbobj))
                    {
                    case 0:
                        MessageBox.Show(this, "添加服务器配置失败,请检查是否有写入权限或文件是否存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;

                    case 2:
                    {
                        DialogResult dialogResult5 = MessageBox.Show(this, "该服务器信息已经存在!你确认是否覆盖当前数据库配置?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                        if (dialogResult5 != DialogResult.Yes)
                        {
                            return;
                        }
                        DbConfig.DelSetting(this.dbobj.DbType, this.dbobj.Server, this.dbobj.DbName);
                        int num = DbConfig.AddSettings(this.dbobj);
                        if (num != 1)
                        {
                            MessageBox.Show(this, "建议卸载当前版本,并删除安装目录后重新安装最新版本!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            return;
                        }
                        break;
                    }
                    }
                    base.DialogResult = DialogResult.OK;
                    base.Close();
                }
            }
            catch (Exception ex2)
            {
                MessageBox.Show(this, ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                LogInfo.WriteLog(ex2);
            }
        }