Skip to main content
adicionou 1787 caracteres ao conteúdo
Fonte Link
rubStackOverflow
  • 7,5mil
  • 13
  • 44
  • 89
using System;
using System.Data.SqlClient;

namespace teste
{
    class Program
    {
        private const string Sqlconnection = @"Server=192.168.0.1;DataBase=master;user id=sa;password=xxx;Connection Timeout=0";

        static void Main(string[] args)
        {
            BackupFullDIff();
        }

        public static void BackupFullDIff()
        {
            var localPastaCopia = @"c:\copia";
            var sqlConn = new SqlConnection(Sqlconnection);
            sqlConn.Open();

            //BACKUP FULL
            var comandoSql =
                string.Format("BACKUP DATABASE NOMEBD TO DISK='{0}' WITH  INIT ,  " +
                              "NOUNLOAD ,  NAME = N'NOMEBD backup',  " +
                              "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");
            var sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();

            //BAKUP DIFERENCIAL
        //    comandoSql =
            string.Format("BACKUP DATABASE SCENOMEBD TO DISK='{0}' WITH  INIT ,  " +
        //              "NOUNLOAD ,  DIFFERENTIAL ,  NAME = N'NOMEBD backup',  " +
        //              "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull@"\minhaCopiaDIFF.BAK");
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();

            var sqlConnlocalPastaRestauraBd = @"c:\dados_restaura";
            //RESTORE FULL
            comandoSql =
                String.Format("RESTORE DATABASE {0} " +
                "FROM DISK='{1}' " +
                "WITH RECOVERY, " +
                "MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
                "MOVE 'NOMEBD_log'  TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaFull.BAK", localPastaRestauraBd);
            sqlComm = new SqlConnectionSqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery(Sqlconnection); 

            //RESTORE FULL e DIFF
            /*
            comandoSql =
                String.Format("RESTORE DATABASE {0} " +
                "FROM DISK='{1}' " +
                "WITH NORECOVERY, " +
                "MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
                "MOVE 'NOMEBD_log'  TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaFull.BAK", localPastaRestauraBd);
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.OpenExecuteNonQuery();
            
        var    comandoSql =
            String.Format("RESTORE DATABASE {0} " +
            "FROM DISK='{1}' " +
            "WITH RECOVERY, " +
            "MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
            "MOVE 'NOMEBD_log'  TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaDiff.BAK", localPastaRestauraBd);
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();
            */

        }
    }
}
class Program
{
    private const string Sqlconnection = @"Server=192.168.0.1;DataBase=master;user id=sa;password=xxx;Connection Timeout=0";

    static void Main(string[] args)
    {
        BackupFullDIff();
    }

    public static void BackupFullDIff()
    {
        var localPastaCopia = @"c:\copia";
        
        //BACKUP FULL
        var comandoSql =
        string.Format("BACKUP DATABASE NOMEBD TO DISK='{0}' WITH  INIT ,  " +
                      "NOUNLOAD ,  NAME = N'NOMEBD backup',  " +
                      "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");

        //BAKUP DIFERENCIAL
        //string.Format("BACKUP DATABASE SCE TO DISK='{0}' WITH  INIT ,  " +
        //          "NOUNLOAD ,  DIFFERENTIAL ,  NAME = N'NOMEBD backup',  " +
        //          "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");

        var sqlConn = new SqlConnection(Sqlconnection);
        sqlConn.Open();
            
        var sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
        sqlComm.ExecuteNonQuery();
    }
}
using System;
using System.Data.SqlClient;

namespace teste
{
    class Program
    {
        private const string Sqlconnection = @"Server=192.168.0.1;DataBase=master;user id=sa;password=xxx;Connection Timeout=0";

        static void Main(string[] args)
        {
            BackupFullDIff();
        }

        public static void BackupFullDIff()
        {
            var localPastaCopia = @"c:\copia";
            var sqlConn = new SqlConnection(Sqlconnection);
            sqlConn.Open();

            //BACKUP FULL
            var comandoSql =
                string.Format("BACKUP DATABASE NOMEBD TO DISK='{0}' WITH  INIT ,  " +
                              "NOUNLOAD ,  NAME = N'NOMEBD backup',  " +
                              "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");
            var sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();

            //BAKUP DIFERENCIAL
            comandoSql =
            string.Format("BACKUP DATABASE NOMEBD TO DISK='{0}' WITH  INIT ,  " +
                      "NOUNLOAD ,  DIFFERENTIAL ,  NAME = N'NOMEBD backup',  " +
                      "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaDIFF.BAK");
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();

            var localPastaRestauraBd = @"c:\dados_restaura";
            //RESTORE FULL
            comandoSql =
                String.Format("RESTORE DATABASE {0} " +
                "FROM DISK='{1}' " +
                "WITH RECOVERY, " +
                "MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
                "MOVE 'NOMEBD_log'  TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaFull.BAK", localPastaRestauraBd);
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery(); 

            //RESTORE FULL e DIFF
            /*
            comandoSql =
                String.Format("RESTORE DATABASE {0} " +
                "FROM DISK='{1}' " +
                "WITH NORECOVERY, " +
                "MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
                "MOVE 'NOMEBD_log'  TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaFull.BAK", localPastaRestauraBd);
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();
            
            comandoSql =
            String.Format("RESTORE DATABASE {0} " +
            "FROM DISK='{1}' " +
            "WITH RECOVERY, " +
            "MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
            "MOVE 'NOMEBD_log'  TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaDiff.BAK", localPastaRestauraBd);
            sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
            sqlComm.ExecuteNonQuery();
            */

        }
    }
}
Fonte Link
rubStackOverflow
  • 7,5mil
  • 13
  • 44
  • 89

Alternativa sem Procedure
Obs: Mudar NOMEBD do comando SQL para o nome do banco.

class Program
{
    private const string Sqlconnection = @"Server=192.168.0.1;DataBase=master;user id=sa;password=xxx;Connection Timeout=0";

    static void Main(string[] args)
    {
        BackupFullDIff();
    }

    public static void BackupFullDIff()
    {
        var localPastaCopia = @"c:\copia";
        
        //BACKUP FULL
        var comandoSql =
        string.Format("BACKUP DATABASE NOMEBD TO DISK='{0}' WITH  INIT ,  " +
                      "NOUNLOAD ,  NAME = N'NOMEBD backup',  " +
                      "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");

        //BAKUP DIFERENCIAL
        //string.Format("BACKUP DATABASE SCE TO DISK='{0}' WITH  INIT ,  " +
        //          "NOUNLOAD ,  DIFFERENTIAL ,  NAME = N'NOMEBD backup',  " +
        //          "NOSKIP ,  STATS = 10,  NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");

        var sqlConn = new SqlConnection(Sqlconnection);
        sqlConn.Open();
            
        var sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
        sqlComm.ExecuteNonQuery();
    }
}