Click here to Skip to main content
15,358,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display a message that, you take a backup of database is 15 day old, please take a backup.
Code for BackUP-
C#
public static void BackupDB(string backupDestinationFilePath)
        {
            try
            {
               // Console.WriteLine("Backup operation started");
                Backup backup = new Backup();
                //Set type of backup to be performed to database
                backup.Action = BackupActionType.Database;
                backup.BackupSetDescription = "BackupDataBase description";
                //Set the name used to identify a particular backup set.
                backup.BackupSetName = "Backup";
                //specify the name of the database to back up
                backup.Database = "DB_Jaggry";
                //Set up the backup device to use filesystem.
                BackupDeviceItem deviceItem = new BackupDeviceItem(
                                                backupDestinationFilePath,
                                                DeviceType.File);
                backup.Devices.Add(deviceItem);

                // Setup a new connection to the data server
                ServerConnection connection = new ServerConnection();               
                Server sqlServer = new Server(@"SNEHA-PC\SQLEXPRESS");
                //Initialize devices associated with a backup operation.
                backup.Initialize = true;
                backup.Checksum = true;               
                backup.ContinueAfterError = true;               
                backup.LogTruncation = BackupTruncateLogType.Truncate;                
                backup.SqlBackup(sqlServer);
               MessageBox.Show("Backup operation succeeded");
            }
            catch (Exception ex)
            {                
                MessageBox.Show(ex.ToString());
            }           
        }
Posted
Updated 13-Sep-12 5:25am
v6
Comments
Kenneth Haugland 13-Sep-12 11:13am
   
So your program want to notify the user that it has been 15 days since he took his backup (of what I dont get SQL server?) and you want to show him a MessageBox? This could normally be configured automatically inside the SQL server, so what type of database are you using?
NABIN SEN 13-Sep-12 11:15am
   
Yes.
Kenneth Haugland 13-Sep-12 11:15am
   
I edited my response slightly, any new comments?
NABIN SEN 13-Sep-12 11:21am
   
sql Server 2005 database
Code for Back Up-
public static void BackupDB(string backupDestinationFilePath)
{
try
{
// Console.WriteLine("Backup operation started");
Backup backup = new Backup();
//Set type of backup to be performed to database
backup.Action = BackupActionType.Database;
backup.BackupSetDescription = "BackupDataBase description";
//Set the name used to identify a particular backup set.
backup.BackupSetName = "Backup";
//specify the name of the database to back up
backup.Database = "DB_Jaggry";
//Set up the backup device to use filesystem.
BackupDeviceItem deviceItem = new BackupDeviceItem(
backupDestinationFilePath,
DeviceType.File);
backup.Devices.Add(deviceItem);

// Setup a new connection to the data server
ServerConnection connection = new ServerConnection();
Server sqlServer = new Server(@"SNEHA-PC\SQLEXPRESS");
//Initialize devices associated with a backup operation.
backup.Initialize = true;
backup.Checksum = true;
backup.ContinueAfterError = true;
backup.LogTruncation = BackupTruncateLogType.Truncate;
backup.SqlBackup(sqlServer);
MessageBox.Show("Backup operation succeeded");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
[no name] 13-Sep-12 11:16am
   
Do you want the text in the messagebox to be that horrible txtspk also? Or can if be real words and complete sentences?
Kenneth Haugland 13-Sep-12 11:19am
   
OMG its been like Four score and seven years ago since you u'd your dat' base man...
[no name] 13-Sep-12 11:20am
   
Lol

1 solution

This should help you doing a back up from C#:
SQL Server Express Automated Backup Console Application C# ADO.NET[^]

To get the last backup date:
http://stackoverflow.com/questions/7583817/check-last-database-backup-date-in-c-sharp-sql-server-2008[^]

Now all that is left is to calculate the days :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900