在 ADO.net SQL 中使用交易

Using Transaction in ADO.net SQL

我是ADO的新手,所以我想问一下我是否正确使用了事务。 这里是代码片段

string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('000001','YoungMcD') ";
string SQL2 = "UPDATE tbl_cust SET custname='OldMcDonald' WHERE cust_id='000001'";
string SQL3 = "SELECT * FROM tbl_supplier WHERE supplier_code ='000001'";

// write connstring
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
// end of connection string

// setting connection
SqlConnection db = new SqlConnection(conn);
SqlTransaction transaction1;

db.Open();
transaction1 = db.BeginTransaction();

try
{
    // insert to table
    SqlCommand Com1 = new SqlCommand(SQL1, db, transaction1);
    Com1.ExecuteNonQuery();

    SqlCommand Com2 = new SqlCommand(SQL2, db, transaction1);
    Com2.ExecuteNonQuery();

    SqlCommand Com3 = new SqlCommand(SQL3, db, transaction1);
    Com3.ExecuteNonQuery();

    transaction1.Commit();

    db.Close();
}
catch
{
    transaction1.Rollback();
    db.Close();
    msg = "error";
    goto endret;
}

对于交易,我应该使用

SqlCommand Com1 = new SqlCommand(SQL1, db, transaction1);

而不是

SqlCommand Com1 = new SqlCommand(SQL1, db);

因为我已经声明在 try{} 声明之前开始交易

编辑:

我明白了,第一种语法是适用的,但是如何有效地使用ADO呢?。我觉得这种方式太直接了。

我发现自己一直这样做是为了插入参数,例如:

string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('" + param1 +"','"+ param2 +"') ";

您应该使用一个命令,并将您的连接包装在一个 Using 块中,以便正确处理。此外,您应该在通过执行 SqlDataReader 提交事务后从 tbl_supplier 读取。我假设您只是想知道在事务提交后有多少行受到影响。

这是您的代码的简化版本。

var conn = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('000001','YoungMcD') ";
string SQL2 = "UPDATE tbl_cust SET custname='OldMcDonald' WHERE cust_id='000001'";

using (SqlConnection connection = new SqlConnection(conn))
{
    connection.Open();
    SqlTransaction sqlTran = connection.BeginTransaction();
    SqlCommand command = connection.CreateCommand();
    command.Transaction = sqlTran;

    try
    {
        command.CommandText = SQL1;
        int rowsAffected = command.ExecuteNonQuery();
        command.CommandText = SQL2;
        rowsAffected += command.ExecuteNonQuery();
        transaction.Commit();
    }
    catch (Exception ex1)
    {
        // Attempt to roll back the transaction.
        try
        {
            transaction.Rollback();
        }
        catch (Exception ex2)
        {
            // This catch block will handle any errors that may have occurred
            // on the server that would cause the rollback to fail, such as
            // a closed connection.
        }
    }
}

自上次以来发生了很多事情 year.Here 我试图简化答案。

string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('000001','YoungMcD') ";
string SQL2 = "UPDATE tbl_cust SET custname='OldMcDonald' WHERE cust_id='000001'";

using (SqlConnection conn = new SqlConnection(ConnStr))
{
    SqlTransaction transaction = null;
    try
    {
        conn.Open();
        transaction = conn.BeginTransaction();
        using (SqlCommand cmd = new SqlCommand(SQL1, conn, transaction)) { cmd.ExecuteNonQuery(); }
        using (SqlCommand cmd = new SqlCommand(SQL2, conn, transaction)) { cmd.ExecuteNonQuery(); }
        transaction.Commit();
        savestats = true;
    }
    catch (Exception ex)
    {
         // Attempt to roll back the transaction.
        try
        {
            transaction.Rollback();
        }
        catch (Exception ex2)
        {
            // This catch block will handle any errors that may have occurred
            // on the server that would cause the rollback to fail, such as
            // a closed connection.
        }
    }
}

之所以在 try{} 之外声明事务,以便我们可以在 catch{} 中回滚它。

此代码的缺点是当 conn.Open() 中发生错误时,无论出于何种原因,然后 transaction.Rollback() 的尝试将导致异常。

这就是为什么要添加另一个 try{} catch{} 来处理它。