Rollback for bulk copy

时光毁灭记忆、已成空白 提交于 2019-12-30 08:16:29

问题


I have an application 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?


回答1:


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();
        }
    }
}


来源:https://stackoverflow.com/questions/2216254/rollback-for-bulk-copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!