开发者

Rollback for bulk copy

开发者 https://www.devze.com 2022-12-19 22:36 出处:网络
I have an application开发者_如何学Go that make a copy from my database by bulk copy class in c#.

I have an application开发者_如何学Go that make a copy from my database by bulk copy class in c#.

Can I rollback the bulk copy action in sql server when occur an exception?


MSDN article: Performing a Bulk Copy Operation in a Transaction or the newer documentation: Transaction and Bulk Copy Operations | Microsoft Docs

using (SqlTransaction transaction = destinationConnection.BeginTransaction())
{
    using (SqlBulkCopy bulkCopy = new SqlBulkCopy( destinationConnection, SqlBulkCopyOptions.KeepIdentity, transaction))
    {
        bulkCopy.BatchSize = 10;
        bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns";

        try
        {
            bulkCopy.WriteToServer(reader);
            transaction.Commit();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            transaction.Rollback();
        }
        finally
        {
            reader.Close();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消