zoukankan      html  css  js  c++  java
  • SqlBulkCopy 参数配置示例

    SqlBulkCopy 参数配置示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    SqlConnection sqlConn = new SqlConnection( sourceConnection) ;
                sqlConn.Open() ;
     
                SqlTransaction tran = sqlConn.BeginTransaction();
     
                SqlCommand commandInsert = new SqlCommand("insert into [test_sqlbulk_update]( b ) values (1) ", sqlConn, tran);
     
                int result = commandInsert.ExecuteNonQuery();
     
                SqlCommand commandSourceData = new SqlCommand("select * from test_sqlbulk ", sqlConn, tran);
                 
                SqlDataReader reader = commandSourceData.ExecuteReader();
                 
                // Set up the bulk copy object using the KeepIdentity option.
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.KeepIdentity, tran))
                {
                    bulkCopy.BatchSize = 2;
                    bulkCopy.DestinationTableName =
                        "dbo.test_sqlbulk_des";
     
                    // Write from the source to the destination.
                    // This should fail with a duplicate key error
                    // after some of the batches have been copied.
                    try
                    {
                        bulkCopy.WriteToServer(reader);
                        reader.Close();
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        reader.Close();
                        tran.Rollback();<br>            // tran.Commit(); 异常仍提交执行,同时注释上一行。
                    }
                    
                }

     => 异常时执行 rollback, 最终结果:没做任何修改.

    => 外部事务,Catch 中执行 Commint ,发生异常,因为自动回滚了,但在Catch 执行 Rollback 无异常。

     View Code

  • 相关阅读:
    没有服务商如何购买ERP的序列号?
    智能ERP主副机设置
    银盒子·序列号购买(2018-12-05)
    简易付微信收款提示支付失败
    简易付无法登录的解决方案
    Orchard详解--第八篇 拓展模块及引用的预处理
    Orchard详解--第七篇 拓展模块(译)
    Orchard详解--第六篇 CacheManager 2
    Orchard详解--第五篇 CacheManager
    Orchard详解--第四篇 缓存介绍
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/8520803.html
Copyright © 2011-2022 走看看