Exemplo n.º 1
0
        public void UMPBackUp()
        {
            try
            {
                var bs          = BackUpSettings.GetBackupSettings("D:\\Configuration\\new");
                var destination = bs.Destination + DateTime.Now.Date.ToShortDateString().Replace('/', '-');
                if (!Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                }
                else
                {
                    Directory.Delete(destination, true);
                    Directory.CreateDirectory(destination);
                }

                var QueryForDBbackup = "Backup database SyncUMP to disk='" + destination;
                QueryForDBbackup += "\\syncUMP.bak'";
                var connection = new SqlConnection(bs.DataSource);
                connection.Open();
                var command = new SqlCommand(QueryForDBbackup, connection);
                command.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 2
0
        public void Restore()
        {
            var bs = BackUpSettings.GetBackupSettings("D:\\Configuration\\new");

            using (var connection = new SqlConnection("Data Source=(local);Integrated Security=true"))
            {
                try
                {
                    connection.Open();
                    var destination = bs.Destination + DateTime.Now.Date.ToShortDateString().Replace('/', '-');
                    var command     =
                        new SqlCommand("RESTORE DATABASE SyncUMP FROM DISK = '" + destination + "\\" + "SyncUMP.bak'",
                                       connection);
                    command.ExecuteNonQuery();
                    if (Directory.Exists(bs.Source))
                    {
                        System.IO.Directory.Delete(bs.Source, true);
                    }
                    connection.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
        }