private void btnOk_Click(object sender, System.EventArgs e) { try { string server = this.comboBoxServer.Text.Trim(); string user = this.txtUser.Text.Trim(); string pass = this.txtPass.Text.Trim(); if ((user == "") || (server == "")) { MessageBox.Show(this, "服务器或用户名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } dbName = this.cmbDBlist.SelectedIndex > 0 ? cmbDBlist.Text : "master"; constr = GetSelVerified() == "Windows" ? "Integrated Security=SSPI;Data Source=" + server + ";Initial Catalog=" + dbName : (pass == "" ? "user id=" + user + ";initial catalog=" + dbName + ";data source=" + server : "user id=" + user + ";password="******";initial catalog=" + dbName + ";data source=" + server); string strtype = GetSelVer(); #region 判断版本 GetSelVer() try { string ver = GetSQLVer(constr); if ((ver == "8") && (strtype == "SQL2005")) { DialogResult dr = MessageBox.Show(this, "该服务器并非SQLServer 2005,是否进行更改选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr == DialogResult.OK) { comboBoxServerVer.SelectedIndex = 2; } } if ((ver == "9") && (strtype == "SQL2000")) { DialogResult dr = MessageBox.Show(this, "该服务器并非SQLServer 2000,是否进行更改选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr == DialogResult.OK) { comboBoxServerVer.SelectedIndex = 1; } } if ((ver == "10") && (strtype != "SQL2008")) { DialogResult dr = MessageBox.Show(this, "该服务器并非SQLServer 2008,是否进行更改选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr == DialogResult.OK) { comboBoxServerVer.SelectedIndex = 0; } } } catch { } #endregion //测试连接 var myCn = new SqlConnection(constr); try { this.Text = "正在连接服务器,请稍候..."; myCn.Open(); } catch (System.Exception ex) { this.Text = "连接服务器失败!"; LogHelper.WriteException(ex); MessageBox.Show(this, "连接服务器失败!请检查服务器地址或用户名密码是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { myCn.Close(); } this.Text = "连接服务器成功!"; if (dbobj == null) { dbobj = new DbSettings(); } //string strtype="SQL2000"; //if (this.comboBoxServerVer.Text == "SQL Server2005") //{ // strtype="SQL2005"; //} //将当前配置写入配置文件 dbobj.DbType = strtype; dbobj.Server = server; dbobj.ConnectStr = constr; dbobj.DbName = dbName; dbobj.ConnectSimple = chk_Simple.Checked; bool succ = DbConfig.AddSettings(dbobj); if (!succ) { MessageBox.Show(this, "该服务器已经存在!请更换服务器地址或检查输入是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } this.DialogResult = DialogResult.OK; this.Close(); } catch (System.Exception ex) { MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); LogHelper.WriteException(ex); } }
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 DbSettings(); } //将当前配置写入配置文件 dbobj.DbType = "Oracle"; dbobj.Server = server; dbobj.ConnectStr = constr; dbobj.DbName = ""; dbobj.ConnectSimple = chk_Simple.Checked; bool succ = DbConfig.AddSettings(dbobj); if (!succ) { MessageBox.Show(this, "该服务器已经存在!请更换服务器地址或检查输入是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //将当前数据库系统类型写入配置 //MainForm.setting.DbType="Oracle"; //ModuleConfig.SaveSettings(MainForm.setting); this.DialogResult = DialogResult.OK; this.Close(); } catch (System.Exception ex) { MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); LogHelper.WriteException(ex); } }