在事务范围内创建和恢复数据库时出现问题
本文关键字:问题 数据库 恢复 事务 范围内 创建 | 更新日期: 2023-09-27 18:02:14
我有一个函数,在这个函数中,我调用了一个存储过程来创建一个新的db,并将现有的db恢复到新创建的db.它在没有事务范围的情况下工作正常,但在事务范围内,它会给出错误,如
Cannot perform a backup or restore operation within a transaction.
BACKUP DATABASE is terminating abnormally.
Cannot perform a backup or restore operation within a transaction.
RESTORE DATABASE is terminating abnormally
示例代码:
using (Transactions.TransactionScope scope = new Transactions.TransactionScope(TransactionScopeOption.Required, new TimeSpan(2, 0, 0)))
{
if (flag == "Yes")
{
SqlCommand cmddb = new SqlCommand("crt_crm_db_details", con);
cmddb.CommandType = CommandType.StoredProcedure;
cmddb.CommandTimeout = 0;
cmddb.Parameters.Add("@custname", SqlDbType.NVarChar).Value = ccode;
cmddb.Parameters.Add("@blank_dbname", SqlDbType.NVarChar).Value = _blankdbCrm;
cmddb.Parameters.Add("@blank_bakpath", SqlDbType.NVarChar).Value = bakRestorePath;
cmddb.Parameters.Add("@blank_mdfpath", SqlDbType.NVarChar).Value = mdfRestorePath;
cmddb.Parameters.Add("@blank_ldfpath", SqlDbType.NVarChar).Value = ldfRestorePath;
cmddb.Parameters.Add("@usercnt", SqlDbType.NVarChar).Value = userCountdb;
SqlParameter Typedb = new SqlParameter("@errorid", SqlDbType.NVarChar);
Typedb.Direction = ParameterDirection.Output;
Typedb.Value = null;
Typedb.Size = 50;
cmddb.Parameters.Add(Typedb);
try
{
con.Open();
cmddb.ExecuteNonQuery();
}
catch (Exception ex)
{
VWLogger.LogMessage("Exception in crt_crm_db_details:", TraceEventType.Critical);
VWLogger.LogMessage(ex, TraceEventType.Critical);
return ex.Message;
throw new CustomSoapException(CustomSoapException.ExceptionCode.AuthenticationException, ex.Message);
}
con.Close();
}
scope.Complete();
return "Success";
}
任何建议吗?
好吧,这个错误很明显,这是不会工作的…"Cannot perform a backup or restore operation within a transaction."
不是很模糊;选择:
- 在事务之外执行tsql
- 如果你可能已经有一个嵌套的事务,使用一个零事务
TransactionScope
(即TransactionScopeOption.Suppress
)
只要在你的查询中删除" begin tran", " Commit tran",那么它就会正常工作。