public static bool NewDatacontexDatabase() { bool result = false; try { string serverhost_SM = O2S_Common.EncryptAndDecrypt.MD5EncryptAndDecrypt.Decrypt(ConfigurationManager.AppSettings["ServerHost"].ToString().Trim() ?? "", true); string serveruser_SM = O2S_Common.EncryptAndDecrypt.MD5EncryptAndDecrypt.Decrypt(ConfigurationManager.AppSettings["Username"].ToString().Trim(), true); string serverpass_SM = O2S_Common.EncryptAndDecrypt.MD5EncryptAndDecrypt.Decrypt(ConfigurationManager.AppSettings["Password"].ToString().Trim(), true); string serverdb_SM = O2S_Common.EncryptAndDecrypt.MD5EncryptAndDecrypt.Decrypt(ConfigurationManager.AppSettings["Database"].ToString().Trim(), true); ServerName = serverhost_SM; ServerCatalog = serverdb_SM; string ConnectionString = "Data Source = " + serverhost_SM + "; Initial Catalog = " + serverdb_SM + ";Persist Security Info=True;User ID=" + serveruser_SM + ";Password="******"select 1", connection); //cmd.ExecuteNonQuery(); //connection.Close(); result = true; } catch (Exception ex) { O2S_Common.Logging.LogSystem.Error("Loi new Datacontext" + ex.ToString()); } return(result); }
/// <summary> /// Kết nối đến cơ sở dữ liệu /// </summary> public static void ConnectToDatabase() { //nạp thông tin kết nối ConnectionString = Settings.Default.ConnectionString; ServerName = Settings.Default.Database_ServerName; ServerCatalog = Settings.Default.Database_ServerCatalog; Database = new QuanLyHocVienDataContext(ConnectionString); //kiểm tra kết nối SqlConnection connection = new SqlConnection(ConnectionString); connection.Open(); SqlCommand cmd = new SqlCommand("select 1", connection); cmd.ExecuteNonQuery(); connection.Close(); }