Fastest way of performing Bulk Update in C# / .NET

前端 未结 3 682
自闭症患者
自闭症患者 2021-02-11 07:35

I\'m trying to figure out whats the best possible way to perform a bulk update via my mini console application in SQL server. I have written my own way of bulk update like follo

3条回答
  •  长发绾君心
    2021-02-11 08:19

    The fastest way would be to bulk insert the data into temporary table using the built in SqlBulkCopy Class, and then update using join to that table

    Or you can use a tool such as SqlBulkTools which does exactly this in an easy way.

    var bulk = new BulkOperations();
    
    using (TransactionScope trans = new TransactionScope())
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=mydb;Integrated Security=SSPI")
        {
            bulk.Setup()
                .ForCollection(items)
                .WithTable("Items")
                .AddColumn(x => x.QuantitySold)
                .BulkUpdate()
                .MatchTargetOn(x => x.ItemID) 
                .Commit(conn);
        }
    
        trans.Complete();
    }
    

提交回复
热议问题