简体   繁体   English

使用事务运行脚本

[英]Run scripts with transaction

I have to execute already written TSQL scripts (*.sql files), that doesn't have transacion. 我必须执行已经编写的TSQL脚本(* .sql文件),该脚本没有事务处理。 I have around 300 scripts for each client (mostly different scripts), and I wrote a little C# application that uses sqlcmd to execute scripts in the given order. 对于每个客户端,我大约有300个脚本(大多数是不同的脚本),并且我编写了一个小C#应用程序,该应用程序使用sqlcmd以给定的顺序执行脚本。

The scripts contain insert\\update\\delete statements and ddl too. 脚本也包含insert \\ update \\ delete语句和ddl。

It runs the scripts one-by-one, and there is a possibility that one of the scripts fails. 它会一一运行脚本,并且其中一个脚本可能会失败。 If one of the scripts fails, it logs which one did, and logs the error message that the sqlcmd gives too. 如果其中一个脚本失败,它将记录哪个脚本失败,并记录sqlcmd给出的错误消息。 I need to rollback at these situations. 在这些情况下,我需要回滚。 I don't have to rollback the scripts that successfully ran, but I should roll back the last one that failed. 我不必回滚成功运行的脚本,但是我应该回滚最后一个失败的脚本。

By rollback I mean I need to restore to the point before the last script even started. 通过回滚,我的意思是我需要恢复到最后一个脚本开始之前的状态。 I don't really know what should I look up to solve this. 我真的不知道该怎么解决这个问题。

Thank you for any help. 感谢您的任何帮助。

If I understood right then this is something you are looking for. 如果我理解正确,那么这就是您要寻找的东西。 As long as you have an open connection and open transaction, you can use this. 只要您有开放的连接和开放的事务,就可以使用它。 It is not perfect, but it is a start. 这不是完美的,但这是一个开始。

public bool GenericSP_PersistentConnection(string strStoredProcedureName, SortedList<string, string> values, SqlConnection s_persistent_conn, SqlTransaction transaction, string returnValueName)
{
    try
    {
        if (strStoredProcedureName == "") throw new Exception(noStoredProcedure);
        if (values == null) throw new Exception(noValue);
        if (returnValueName == "") throw new Exception(noOutputParameterName);

        //If Connection object is null, create a new on
        if (s_persistent_conn == null) s_persistent_conn = new SqlConnection(GetConnectionString());

        //Create command object
        s_comm = new SqlCommand(strStoredProcedureName, s_persistent_conn);
        s_comm.CommandType = CommandType.StoredProcedure;
        s_comm.Transaction = transaction;
        s_comm.CommandTimeout = 600;
        s_adap = new SqlDataAdapter(s_comm);

        ////set up the parameters for the stored proc
        foreach (var item in values)
            s_comm.Parameters.AddWithValue("@" + item.Key, item.Value);
        s_comm.Parameters.AddWithValue("@" + returnValueName, 0);
        s_comm.Parameters["@" + returnValueName].Direction = ParameterDirection.Output;

        //Excecute call to DB
        s_comm.ExecuteNonQuery();

        outputValue = s_comm.Parameters["@" + returnValueName].Value.ToString();
        }
    catch (Exception ex)
    {
        // If something happens, rollback the whole transaction.
        transaction.Rollback();
        throw ex;
    }
    finally
    {
            s_comm.Dispose();
            s_adap.Dispose();
    }
    return true;
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM