简体   繁体   English

关于SQl BulkCopy的问题

[英]Questions About SQl BulkCopy

I am wondering how can do a mass insert and bulk copy at the same time? 我想知道如何同时批量插入和批量复制? I have 2 tables that should be affect by the bulk copy as they both depend on each other. 我有2个表应受到批量复制的影响,因为它们彼此依赖。

So I want it that if while inserting table 1 a record dies it gets rolled back and table 2 never gets updated. 因此,我希望如果在插入表1时一条记录死亡,它会回滚,而表2永远不会更新。 Also if table 1 inserts good and table 2 an update fails table 1 gets rolled back. 同样,如果表1插入良好,而表2更新失败,则表1会回滚。

Can this be done with bulk copy? 可以使用批量复制吗?

Edit 编辑

I should have mentioned I am doing the bulk insert though C#. 我应该提到我正在通过C#进行批量插入。

It sort of looks like this but this is an example I been working off. 看起来像这样,但这是我一直在努力的一个例子。 So I am not sure if I have to alter it to be a stored procedure(not sure how it would look and how the C# code would look) 所以我不确定是否必须将其更改为存储过程(不确定其外观以及C#代码的外观)

private static void BatchBulkCopy()
{
    // Get the DataTable 
    DataTable dtInsertRows = GetDataTable();

    using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = "TBL_TEST_TEST";

        // Number of records to be processed in one go
        sbc.BatchSize = 500000;

        // Map the Source Column from DataTabel to the Destination Columns in SQL Server 2005 Person Table
        // sbc.ColumnMappings.Add("ID", "ID");
        sbc.ColumnMappings.Add("NAME", "NAME");

        // Number of records after which client has to be notified about its status
        sbc.NotifyAfter = dtInsertRows.Rows.Count;

        // Event that gets fired when NotifyAfter number of records are processed.
        sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);

        // Finally write to server
        sbc.WriteToServer(dtInsertRows);
        sbc.Close();
    }

}

You can run bulk inserts inside of a user defined transaction so do something like this: 您可以在用户定义的事务内运行批量插入,因此请执行以下操作:

BEGIN TRANSACTION MyDataLoad
BEGIN TRY

BULK INSERT ...

BULK INSERT ...

COMMIT TRANSACTJION MyDataLoad
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION
END CATCH

However, there may be other ways to accomplish what you want. 但是,可能还有其他方法可以完成您想要的事情。 Are the tables empty before you bulk insert into them? 在批量插入表之前,表是否为空? When you say the tables depend on each other, do you mean that there are foreign key constraints you want enforced? 当您说这些表相互依赖时,是否意味着您要强制执行外键约束?

I am wondering how can do a mass insert and bulk copy at the same time? 我想知道如何同时批量插入和批量复制? I have 2 tables that should be affect by the bulk copy as they both depend on each other. 我有2个表应受到批量复制的影响,因为它们彼此依赖。 So I want it that if while inserting table 1 a record dies it gets rolled back and table 2 never gets updated. 因此,我希望如果在插入表1时一条记录死亡,它会回滚,而表2永远不会更新。 Also if table 1 inserts good and table 2 an update fails table 1 gets rolled back. 同样,如果表1插入良好,而表2更新失败,则表1会回滚。 Can this be done with bulk copy? 可以使用批量复制吗?

No - the whole point of SqlBulkCopy is to get data into your database as fast as possible. 否-SqlBulkCopy的全部目的是使数据尽快进入数据库。 It will just dump the data into a single table. 它将仅将数据转储到单个表中。

The normal use case will be to then inspect that table once it's imported, and begin to "split up" that data and store it into whatever place it needs to go - typically through a stored procedure (since the data already is on the server, and you want to distribute it to other tables - you don't want to pull all that data back down to the client, inspect it, and then send it back to the server one more time). 正常的用例是在导入该表之后对其进行检查,然后开始“拆分”该数据并将其存储到所需的任何位置-通常通过存储过程(因为数据已经在服务器上,并且您想将其分发到其他表-您不想将所有数据拉回客户端,对其进行检查,然后再将其发送回服务器)。

SqlBulkCopy only grabs a bunch of data and drops it into a table - very quickly so. SqlBulkCopy只获取一堆数据并将其放入表中-很快。 It cannot split up data into multiple tables based on criteria or conditions. 不能根据条件或条件将数据分成多个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM