0

I'm doing backup of database through code:

using (var connection = new SqlConnection(_connectionString))
{
       var query = String.Format("BACKUP DATABASE [{0}] TO DISK='{1}'", databaseName, fileName);

       using (var command = new SqlCommand(query, connection))
       {
           connection.Open();
           command.ExecuteNonQuery();
       }
}

The return value od ExecuteNonQuery() is useless because I get as result -1. Is there any other way do detect when the backup is done?

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Josef
  • 2,648
  • 5
  • 37
  • 73
  • https://stackoverflow.com/a/38060528/2946329 – Salah Akbari Jan 16 '20 at 08:48
  • 3
    https://stackoverflow.com/questions/5266833/should-i-use-executenonquery-for-this-db-backup-command – Salah Akbari Jan 16 '20 at 08:48
  • you can always watch the file and check if the last modified was updated – styx Jan 16 '20 at 08:49
  • Does this answer your question? [Is there a SQL script that I can use to determine the progress of a SQL Server backup or restore process?](https://stackoverflow.com/questions/152447/is-there-a-sql-script-that-i-can-use-to-determine-the-progress-of-a-sql-server-b) – Venkataraman R Jan 16 '20 at 08:52

1 Answers1

1

You can either check if the file is in the destination or you can wrap your code in try catch and set a flag success = false when there is an exception.

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42