Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally

I am trying to execute backUp and restore command in my application that it modeled by EF6.

For my backup I create a Sp as you can see here:

CREATE PROCEDURE GetBackUp
    -- Add the parameters for the stored procedure here
    @address nvarchar(max)
AS
BEGIN
    BACKUP DATABASE [db-invoice-169] to DISK=@address
END
GO

In my application I call this Sp

 private void Backup_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            string str_filename = string.Empty;
            sfd.FileName = "backup_database_" + DateTime.Now.ToShortDateString().Replace("/", "_");
            sfd.Filter = @"backup files(*.bak)|*.bak|all files(*.*)|*.*";
            sfd.FilterIndex = 1;
            sfd.OverwritePrompt = true;
            sfd.Title = "***save backup files***";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                str_filename = sfd.FileName;
                backup(str_filename);
            }
        }
        private void backup(string str_filename)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                db.Database.ExecuteSqlCommand(@"EXEC [dbo].[back_up] @address = N'"+str_filename+"'");
                this.Cursor = Cursors.Default;
                MessageBox.Show("عملیات پشتیبان گیری موفقیت آمیز بود");
            }
            catch (Exception ex)
            {
                MessageBox.Show("عملیات پشتیبان گیری موفقیت آمیز نبود |" + ex.Message);
            }

        }

But I got this error:

Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally.

like image 425
Ehsan Akbar Avatar asked Aug 14 '14 14:08

Ehsan Akbar


Video Answer


1 Answers

Try changing this line:

db.Database.ExecuteSqlCommand(@"EXEC [dbo].[back_up] @address = N'"+str_filename+"'");

to:

db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, @"EXEC [dbo].[back_up] @address = N'"+str_filename+"'");

Abrar

like image 59
abrar Avatar answered Sep 30 '22 05:09

abrar