如何回滚事务SQL Server脚本?

jchrr9hc  于 2022-12-17  发布在  SQL Server
关注(0)|答案(1)|浏览(230)

我正在使用DbUp(Documentation)包来维护和执行数据库上的脚本。

var builder = DeployChanges.To
                        .SqlDatabase(connectionString)
                        .WithExecutionTimeout(TimeSpan.FromSeconds(300))
                        .WithTransactionPerScript()
                        .WithScriptsFromFileSystem(rootPath, new FileSystemScriptOptions { IncludeSubDirectories = true })

我想回滚所有的事务,如果任何脚本失败,同时执行一堆脚本。

WithTransactionAlwaysRollback()

即使所有脚本都成功执行,事务也会回滚。我只想在失败时回滚。
有没有别的办法解决这个问题?

jmp7cifd

jmp7cifd1#

用途

.WithTransaction()

代替

.WithTransactionPerScript()

下面是这些扩展方法的实现:

public static UpgradeEngineBuilder WithTransaction(this UpgradeEngineBuilder builder)
{
    builder.Configure(delegate (UpgradeConfiguration c)
    {
        c.ConnectionManager.TransactionMode = TransactionMode.SingleTransaction;
    });
    return builder;
}

public static UpgradeEngineBuilder WithTransactionPerScript(this UpgradeEngineBuilder builder)
{
    builder.Configure(delegate (UpgradeConfiguration c)
    {
        c.ConnectionManager.TransactionMode = TransactionMode.TransactionPerScript;
    });
    return builder;
}

相关问题