Methoun Ahmed

Methoun Ahmed

  • NA
  • 145
  • 101.9k

Backup Database In Sql database C#

Oct 4 2017 10:38 AM
 Whats wrong the code
 
string database = con.Database.ToString();
try
{
if(textBox1.Text==string.Empty)
{
MessageBox.Show("Enter your backup file location");

}
else
{
string cmd = "BACKUP DATABASE[" + database + "] TO DISK '" + textBox1.Text + "\\" + "Database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".back'";
using(SqlCommand cmd2=new SqlCommand(cmd,con))
{
if(con.State!=ConnectionState.Open)
{
con.Open();
}
cmd2.ExecuteNonQuery();
con.Close();
MessageBox.Show("Database Backup Successfully");
btnBackup.Enabled = false;
}

}
}
catch
{

}

Answers (5)