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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With