I have just used this code in my Project:
private void DatabaseBackup()
{
string strDate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0');
string[] strTime = DateTime.Now.TimeOfDay.ToString().Split(':');
string strDateTime = strDate + " " + strTime[0].PadLeft(2, '0') + strTime[1].PadLeft(2, '0');
try
{
string backupPath = txtBackupLocation.Text.Trim() + "\\" + strDateTime + ".bak";
int retVal = 0;
retVal = _settingManager.DatabaseBackup(backupPath);
if (retVal != 0)
{
btnBackupDatabase.Enabled = false;
lblBackupStatus.Text = "Database backup successful.";
MessageBox.Show("The backup of database completed successfully." + Environment.NewLine + backupPath, Program.MESSAGEBOXTITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
btnClose.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show("Error [Database Backup]: " + Environment.NewLine + ex.Message, Program.MESSAGEBOXTITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
lblBackupStatus.Text = "Database backup failed.";
}
}
For more have look on:
SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[
^]
http://midnightprogrammer.net/post/BackupRestore-SQL-database-using-C.aspx[
^]