大家好我想在我的应用程序中执行backUp
和restore
EF6
对于我的备份,我可以在此处创建Sp
:
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
在我的应用程序中,我称之为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);
}
}
但我收到了这个错误:
Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally.
答案 0 :(得分:18)
尝试更改此行:
db.Database.ExecuteSqlCommand(@"EXEC [dbo].[back_up] @address = N'"+str_filename+"'");
为:
db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, @"EXEC [dbo].[back_up] @address = N'"+str_filename+"'");
阿布拉
答案 1 :(得分:1)
在备份代码之前尝试一下
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress))
{
// Add you backup query or your stuff
ts.Complete();
}
答案 2 :(得分:-1)
根据亚伯回答,我在SQLlocaldb和Entity Framework中的还原过程中遇到此错误:
RESTORE cannot process database 'database' because it is in use by this session. It is recommended that the master database be used when performing this operation.
RESTORE DATABASE is terminating abnormally.
解决方案:
db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, @"USE master EXEC [dbo].[back_up] @address = N'"+str_filename+"'")