ADO.Net存储过程transaction的使用事项

public bool InsertInvitation(DataTable dt)
        {
            SqlTransaction transaction = null;
            try
            {
                using (SqlConnection conn = new SqlConnection(sqlHelper.SQLConString))
                {


                    conn.Open();
                    transaction = conn.BeginTransaction();
                    SqlBulkCopy sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.CheckConstraints, transaction);
                    //服务器上目标表的名称
                    sbc.DestinationTableName = dt.TableName;
                    sbc.BatchSize = dt.Rows.Count;
                    sbc.BulkCopyTimeout = 180;
                    sbc.WriteToServer(dt);

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    cmd.Transaction = transaction;
                    cmd.CommandText = "insert AABB(ID)values(1)";
                    cmd.ExecuteNonQuery();
                    Convert.ToInt32("sdfs");
                    transaction.Rollback();

                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                LogMsg.WriteLogToApplicationFolderByMonth(ex.ToString());
                return false;
            }
            return true;
        }

当报异常的时候,transaction会引发“此 SqlTransaction 已完成;它再也无法使用。”

 

改成下面的样子则无碍。另外,使用事务的时候,要注意将SqlTransaction赋给执行命令的SqlTransaction,如

SqlBulkCopy sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.CheckConstraints, transaction);、cmd.Transaction = transaction;
public bool InsertInvitation(DataTable dt)
        {
            using (SqlConnection conn = new SqlConnection(sqlHelper.SQLConString))
            {
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    SqlBulkCopy sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.CheckConstraints, transaction);
                    //服务器上目标表的名称
                    sbc.DestinationTableName = dt.TableName;
                    sbc.BatchSize = dt.Rows.Count;
                    sbc.BulkCopyTimeout = 180;
                    sbc.WriteToServer(dt);

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    cmd.Transaction = transaction;
                    cmd.CommandText = "insert AABB(ID)values(1)";
                    object i=cmd.ExecuteNonQuery();
                    if(Convert.toInt32(i)<=0){
                      transaction.Rollback();
                    }
                    
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    LogMsg.WriteLogToApplicationFolderByMonth(ex.ToString());
                    return false;
                }
            }

            return true;
        }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值