2

I have an c# winforms application (.net 2 framework). I need to backup data bases from my application. I am trying to do this by executing an SqlCommand asynchronously. The code is executed with no exceptions but I dont get the .bak file in my destination...

this is the code :

#region backup DB using T-SQL command

string connString = "Data Source=" + ConfigurationManager.AppSettings.Get("localhost_SQLEXPRESS") + ";Initial Catalog=" + db + ";UserID=" + ConfigurationManager.AppSettings.Get("user") + ";Password=" + ConfigurationManager.AppSettings.Get("password");
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connString);
builder.AsynchronousProcessing = true;
using (SqlConnection sqlConnection1 = new SqlConnection(builder.ConnectionString))
{
    using (SqlCommand cmd = new SqlCommand("BACKUP DATABASE " + db + " TO DISK=" + location + "\\" + ConfigurationManager.AppSettings.Get("DataBaseBackupsFolderName") + "\\" + db + ".bak'", sqlConnection1))
    {    
        sqlConnection1.Open();    
        IAsyncResult result = cmd.BeginExecuteNonQuery();

        while (!result.IsCompleted)
        {
            Thread.Sleep(100);
        }
    }
}
#endregion
4
  • Could you put a breakpoint on the sqlConnection1.Open() and check the value of cmd.CommandText (in particular the filename path) Commented Jul 4, 2012 at 10:10
  • 2
    Just to make sure... The backup is stored on the server, not on the computer that issued the command (except if both are the same machine). Commented Jul 4, 2012 at 10:16
  • The backup is on the computer... Commented Jul 4, 2012 at 10:46
  • @Steve This is the command - BACKUP DATABASE DB_Tags_TestingTagBurner TO DISK=D:\New folder\DataBaseBackups\DB_Tags_TestingTagBurner.bak' Commented Jul 4, 2012 at 10:50

3 Answers 3

3

In your SQL backup line you seem to be missing a single quote at the beginning of the path to the backup file.

  using (SqlCommand cmd = new SqlCommand("BACKUP DATABASE " + db + " TO DISK='" + location + "\\" + ConfigurationManager.AppSettings.Get("DataBaseBackupsFolderName") + "\\" +db + ".bak'", sqlConnection1)) 
Sign up to request clarification or add additional context in comments.

Comments

1

Two advices to try to isolate the problem:

1) Get the resulting string (the one you are executing on the SqlCommand and run it manually on SQL Server to make sure the backup commnad is correct.

2) Try a synchronous command with a regular ExecuteNonQuery to see if you are getting a SQL Server exception

Comments

1

You should call EndExecuteNonQuery() on your SqlCommand instance in order to throw any eventual exception and thus understand what is wrong with your SQL statements:

IAsyncResult result = cmd.BeginExecuteNonQuery();

// Wait for the command to complete

result.AsyncWaitHandle.WaitOne();

// End the execution and throw any eventual exception

cmd.EndExecuteNonQuery(result);

As you can see, I have also replaced your original Thread.Sleep() cycle block with a more effective wait on the wait handle of the command.

Quoting MSDN:

For each call to BeginOperationName, the application should also call EndOperationName to get the results of the operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.