public static bool AutoBackUp() { try { GeneralInfo info = new GeneralInfo(); info.LoadAll(); string path = GetDBAutoBackupPath() + "\\" + info.HospitalName + "PharmaInventory" + DateTime.Now.ToString("MMMddyyyy") + ".bak"; string connectionString = ""; try { connectionString = Program.ConnStringManager.GetFromRegistry(); } catch { connectionString = ConfigurationManager.AppSettings["dbConnection"].ToString(); } System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString); System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(); if (conn.State != ConnectionState.Open) conn.Open(); string dbName = conn.Database; com.CommandText = "BACKUP DATABASE [" + dbName + "] TO DISK = N'" + path + "' WITH NOFORMAT, NOINIT, NAME = N'PharmaInventory" + DateTime.Now.ToString("MMMddyyyy") + "-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"; com.Connection = conn; com.ExecuteNonQuery(); info.LoadAll(); info.LastBackUp = DateTime.Now; info.Save(); // XtraMessageBox.Show("Backup completed to " + path + "!", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } catch(Exception ex) { //XtraMessageBox.Show("Auto Backup has failed! Backup the database manually.", "Backup the database manually", MessageBoxButtons.OK, MessageBoxIcon.Error); } return false; }
private void bw_DoWork(object sender, DoWorkEventArgs e) { int userID = Convert.ToInt32(e.Argument); BLL.User user = new User(); user.LoadByPrimaryKey(userID); try { HelperClasses.DirectoryServices.RefreshFromDirectoryServices(); BLL.GeneralInfo info = new GeneralInfo(); info.LoadAll(); info.LastSync = DateTime.Now; info.Save(); e.Result = "Completed!"; } catch (Exception ex) { if (user.UserType != UserType.Constants.SYSTEM_ADMIN) e.Result = "There has been a network error. The database is not in sync with directory services. Please connect to the Internet and try again."; else e.Result = ex.Message; } }
/// <summary> /// Takes the database backup path from the registry /// </summary> private static void BackUpDatabase() { try { GeneralInfo info = new GeneralInfo(); info.LoadAll(); string path = GetDBAutoBackupPath() + "\\" + info.HospitalName + "PharmaInventory" + DateTime.Now.ToString("MMMddyyyy") + ".bak"; string connectionString = PharmInventory.HelperClasses.DatabaseHelpers.GetConnectionString(); System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString); System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(); if (!(conn.State == ConnectionState.Open)) conn.Open(); string dbName = conn.Database; com.CommandText = "BACKUP DATABASE [" + dbName + "] TO DISK = N'" + path + "' WITH NOFORMAT, NOINIT, NAME = N'PharmaInventory" + DateTime.Now.ToString("MMMddyyyy") + "-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"; com.Connection = conn; com.ExecuteNonQuery(); info.LoadAll(); info.LastBackUp = DateTime.Now; info.Save(); XtraMessageBox.Show("Backup completed to " + path + "!", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { XtraMessageBox.Show("Backup has failed! Please Try Again.", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Error); } }