private void btn_Connect_Click(object sender, EventArgs e) { // @"data source=KHIEM-PC\SQLEXPRESS" + ";initial catalog=QLKhoDienLuc" + ";persist security info=True;user id=sa" + ";password=2051990" + ";MultipleActiveResultSets=True;"; StringBuilder Con = new StringBuilder("Data Source="); Con.Append(txtServerName.Text); Con.Append(";Initial Catalog="); Con.Append(txtTenCSDL.Text); if (String.IsNullOrEmpty(txtUser.Text) && String.IsNullOrEmpty(txtPwd.Text)) Con.Append(";Integrated Security=true;"); else { Con.Append(";persist security info=True"); Con.Append(";User Id="); Con.Append(txtUser.Text); Con.Append(";Password="******";MultipleActiveResultSets=True;"); } string strCon = Con.ToString(); DatabaseHelper help = new DatabaseHelper(); if (help.CheckConnection(strCon) == 0) { MessageBox.Show("Kết nối thất bại vui lòng kiểm tra lại thông tin "); return; } else { Utilities.clsThamSoUtilities.connectionString = strCon; help.CloseDatabase(); updateConfigFile(strCon); this.Close(); } }
private void DocFileCauHinh() { try { XmlDocument XmlDoc = new XmlDocument(); //Loading the Config file XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); // XmlDoc.Load("App.config"); foreach (XmlElement xElement in XmlDoc.DocumentElement) { if (xElement.Name == "connectionStrings") { //setting the coonection string var temp = xElement.LastChild.Attributes[1].Value; try { DatabaseHelper help = new DatabaseHelper(); if (help.CheckConnection((string)temp) == 0) { frmKetNoi frm = new frmKetNoi(); frm.MdiParent = this; // Display the new form. frm.Show(); } else { Utilities.clsThamSoUtilities.connectionString = temp; help.CloseDatabase(); }// } catch (Exception ex) { } } if (xElement.Name == "appSettings") { bool temp = bool.Parse(xElement.ChildNodes.Item(0).Attributes[1].Value); if (temp == true) { frmDangNhap frm = new frmDangNhap(); frm.MdiParent = this; // Display the new form. frm.Show(); return; } Utilities.clsThamSoUtilities.isSectionLogin = true; } } //writing the connection string in config file // XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); //clsThamSoUtilities.ID_Kho = int.Parse(config.AppSettings.Settings["IDkho"].Value.ToString()); } catch (Exception ex) { // MessageBox.Show("Chưa cấu hình CSDL! Vui lòng cấu hình hệ thống trước."); } }