/// <summary> /// 连接测试 /// </summary> private void btnTestConn_Click(object sender, EventArgs e) { if (this.cmbSqlServers.Text == "" || this.cmbAllDataBases.Text == "" || this.txtUserName.Text == "" || this.txtPassword.Text == "") { MessageBox.Show("数据库名称及用户密码不能为空!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { string strconn = "Data Source={0};User ID={1};Password={2};Initial Catalog={3};Pooling=true"; strconn = string.Format(strconn, this.cmbSqlServers.Text.Trim(), this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim(), this.cmbAllDataBases.Text.Trim()); string strsql = string.Empty; strsql = "select 1"; BCommon bcomm = new BCommon(); if (bcomm.IsDBConn(strconn, strsql)) { MessageBox.Show("连接成功!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("连接失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); Logger.Error("数据库连接失败!", ex); } }
/// <summary> /// init 页面初始化 /// </summary> private void FrmLogin_Load(object sender, EventArgs e) { this.TransparencyKey = Color.WhiteSmoke; panelDBConfig.BackColor = Color.FromArgb(65, 204, 212, 230); try { string strconn = string.Empty; strconn = AppXmlTool.ReadXmlFiles("ConnectionString"); string ConStringEncrypt = AppXmlTool.ReadXmlFiles("ConStringEncrypt"); if (ConStringEncrypt == "true") { strconn = DESEncrypt.Decrypt(strconn); } string[] ary = strconn.Split(';'); if (ary.Length > 3) { this.cmbSqlServers.Text = ary[0].Substring(ary[0].IndexOf("=") + 1).Trim(); this.txtUserName.Text = ary[1].Substring(ary[1].IndexOf("=") + 1).Trim(); this.txtPassword.Text = ary[2].Substring(ary[2].IndexOf("=") + 1).Trim(); this.cmbAllDataBases.Text = ary[3].Substring(ary[3].IndexOf("=") + 1).Trim(); } } catch (Exception ex) { Logger.Error("数据库配置文件文读取异常!", ex); } try { string strconn = "Data Source={0};User ID={1};Password={2};Initial Catalog={3};Pooling=true"; strconn = string.Format(strconn, this.cmbSqlServers.Text.Trim(), this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim(), this.cmbAllDataBases.Text.Trim()); string strsql = string.Empty; strsql = "select 1"; BCommon bcomm = new BCommon(); if (bcomm.IsDBConn(strconn, strsql)) { DataTable dt = bcomm.GetMasterList("COMPANY", "", "", "").Tables[0]; cboCompany.DisplayMember = "NAME_SHORT"; cboCompany.ValueMember = "CODE"; cboCompany.DataSource = dt; txtLoginUserCode.Focus(); } else { MessageBox.Show("数据库连接失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); panelDBConfig.Visible = true; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); Logger.Error("数据库连接失败!", ex); } }