Hi rani,
Refer the below code.
public void BackupDataBase(string dbName, string destinationPath)
{
//Check that directory already there otherwise create
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
try
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
//query to take backup database
string query = "backup database " + dbName + " to disk='" + destinationPath + "\\" + dbName + "_" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'";
sqlcmd = new SqlCommand(query, con);
sqlcmd.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "MessagePopUp", "alert('Backup database successfully To " + destinationPath.Replace(":", ":\\") + "');", true);
}
catch
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "MessagePopUp", "alert('Error During backup database!');", true);
}
}