public void GetBackup()
{
try
{
String MontName = "";
int Month = DateTime.Today.Month;
switch (Month)
{
case 1:
MontName = "JAN";
break;
case 2:
MontName = "FEB";
break;
case 3:
MontName = "MAR";
break;
case 4:
MontName = "APR";
break;
case 5:
MontName = "MAY";
break;
case 6:
MontName = "JUN";
break;
case 7:
MontName = "JUL";
break;
case 8:
MontName = "AUG";
break;
case 9:
MontName = "SEP";
break;
case 10:
MontName = "OCT";
break;
case 11:
MontName = "NOV";
break;
case 12:
MontName = "DEC";
break;
}
string MonthYear = MontName + "_" + DateTime.Today.Year;
string targetPath = @ "E:\DatabaseBackup";
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
targetPath = @ "E:\DatabaseBackup\Data_Backup_" + MonthYear;
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
string Date = DateTime.Today.Day + "_" + DateTime.Today.Month + "_" + DateTime.Today.Year;
SqlConnection cn = new SqlConnection("Your connection string here");
string STRSQL = @ "BACKUP DATABASE TEST TO DISK='" + targetPath + "\\TEST_" + Date + ".bak'";
cn.Open();
SqlCommand cmd = new SqlCommand(STRSQL, cn);
cmd.ExecuteNonQuery();
cn.Close();
}
catch (SqlException ex)
{
MessageBox.Show("DataBase Backup : " + ex.Message);
}
}This code snippet show Function for database backup of MS SQL Server using C#.
