private void groupBox2_Leave(object sender, EventArgs e) { cbDatabase.DataSource = null; if (rbIntergrated.Checked) { txtUser.Tag = txtPassword.Tag = ""; if (techlinkErrorProvider1.Validate(this)) { SqlAccessor.Instance().SetConnection(txtServer.Text, "", ""); cbDatabase.DataSource = SqlAccessor.Instance().GetDatabases(); cbDatabase.DisplayMember = "Name"; cbDatabase.ValueMember = "Name"; } } else { txtUser.Tag = txtPassword.Tag = "required"; if (techlinkErrorProvider1.Validate(this)) { SqlAccessor.Instance().SetConnection(txtServer.Text, txtUser.Text, txtPassword.Text); cbDatabase.DataSource = SqlAccessor.Instance().GetDatabases(); cbDatabase.DisplayMember = "Name"; cbDatabase.ValueMember = "Name"; } } }
private void frmConfigSQL_Load(object sender, EventArgs e) { this.Text = "Cau hinh ket noi CSDL" + ConstantInfo.MESSAGE_TITLE + GlobalInfo.CompanyName; cbDatabase.DataSource = null; if (rbIntergrated.Checked) { txtUser.Tag = txtPassword.Tag = ""; if (techlinkErrorProvider1.Validate(this)) { SqlAccessor.Instance().SetConnection(txtServer.Text, "", ""); cbDatabase.DataSource = SqlAccessor.Instance().GetDatabases(); cbDatabase.DisplayMember = "Name"; } } else { txtUser.Tag = txtPassword.Tag = "required"; if (techlinkErrorProvider1.Validate(this)) { SqlAccessor.Instance().SetConnection(txtServer.Text, txtUser.Text, txtPassword.Text); cbDatabase.DataSource = SqlAccessor.Instance().GetDatabases(); cbDatabase.DisplayMember = "Name"; } } }
private void button1_Click(object sender, EventArgs e) { var isSuccess = false; if (techlinkErrorProvider1.Validate(this)) { if (cbDatabase.Text.Trim().Length == 0) { cbDatabase.Focus(); return; } if (rbIntergrated.Checked) { SqlAccessor.Instance().SetConnection(txtServer.Text, cbDatabase.Text, "", ""); } else { SqlAccessor.Instance().SetConnection(txtServer.Text, cbDatabase.Text, txtUser.Text, txtPassword.Text); } var listDb = SqlAccessor.Instance().GetDatabases(); if (listDb.Any(x => x.Name == cbDatabase.Text)) { isSuccess = true; } if (isSuccess) { //Save config var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings"); var cnn = string.Format("metadata=res://*/ModelEcusDeclaration.csdl|res://*/ModelEcusDeclaration.ssdl|res://*/ModelEcusDeclaration.msl;provider=System.Data.SqlClient;provider connection string='{0}'", SqlAccessor.Instance().ConnectionString); if (connectionStringsSection.ConnectionStrings["dbEcusDeclaration"] == null) { connectionStringsSection.ConnectionStrings.Add(new ConnectionStringSettings("dbEcusDeclaration", Utilities.Common.Encrypt(cnn, true))); } else { connectionStringsSection.ConnectionStrings["dbEcusDeclaration"].ConnectionString = Utilities.Common.Encrypt(cnn, true); } config.Save(); ConfigurationManager.RefreshSection("connectionStrings"); this.Close(); } else { MessageBox.Show("Kết nối cơ sở dữ liệu không thành công"); } } }