0

I am trying to make a backup file using this :

 SqlConnection con = new SqlConnection(connection.ConnectionString);
 string fname = ofd.FileName + DateTime.Now.ToString("ddMMyyyy") + " - " + 
 DateTime.Now.ToString("ss-mm-hh");                
 con.Open();
 SqlCommand cmd = new SqlCommand(@"backup database [" + System.Windows.Forms.Application.StartupPath + "\\ManageArtsDB.mdf] to disk ='" + fname + ".bak'with init,stats=10", con);
 cmd.ExecuteNonQuery();
 con.Close();
 MessageBox.Show("Backup database was successfull", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);

The error i get : System.Data.SqlClient.SqlException: "Database" 'C:\Users\Khalid\Desktop\ManageTrainingArtsF\ManageTrainingArts\bin\Debug\ManageArtsDB.mdf' does not exist. Make sure that the name is entered correctly.

the database ManageArtsDB.mdf exist :( but it say its not !

umasankar
  • 599
  • 1
  • 9
  • 28
  • `backup database` expects the name of the database not its physical path E.g. `backup database [ManageArtsDB] to ...` (`con`/`cmd` should be in `using()` blocks) – Alex K. Aug 05 '17 at 12:52
  • My database is local db when i use the line : string strQuery = "Backup database ManageArtsDB to disk = '" + fname + ".bak'"; i got error also :( – brsteej Aug 05 '17 at 13:00
  • You move the code in another Drive and check once. if any Permission issue in for accessing in c drive or Desktop – umasankar Aug 05 '17 at 13:41
  • Possible duplicate of [How to backup a SQL Server 2014 Express Localdb (.mdf) file programmatically](https://stackoverflow.com/questions/35788119/how-to-backup-a-sql-server-2014-express-localdb-mdf-file-programmatically) – Alex Aug 05 '17 at 22:18
  • Done ,, I just deleted the database .. create new local db .. new dataset .. ok – brsteej Aug 06 '17 at 13:22

0 Answers0