简体   繁体   English

参数化多行插入到SQL Server

[英]Parameterized Multi-Row Insert into SQL Server

I'm facing a situation where I need to insert as many as 17,000 rows into a SQL Server database all at once. 我面临一种情况,我需要一次将多达17,000行插入到SQL Server数据库中。 I've come up with two ways to do that, but I'm getting tripped up over query parameterization in C#. 我提出了两种方法来实现此目的,但是我因使用C#中的查询参数化而陷入困境。

One caveat: I'm worried about performance, but not to the exclusion of all else -- if the operation I'm doing takes 8 minutes, then whether I add another 5 seconds vs. 10 seconds for the data insert doesn't really matter. 一个警告:我担心性能,但是不排除所有其他问题-如果我正在执行的操作需要8分钟,那么我是否还要再添加5秒还是10秒来插入数据,并不是真的物。 If the data insert takes an additional 10 minutes on top of the initial operation, that's a big deal. 如果在初始操作之外再花费10分钟插入数据,那就太麻烦了。

Option 1: Multi-Row Insert (chunked) 选项1:多行插入(分块)

The first option is to do a simple multi-row insert: 第一种选择是进行简单的多行插入:

INSERT INTO MyTable (FirstName, LastName, Age)
   VALUES
      ('Bob', 'Smith', 24),
      ('Sally', 'Jones', 32),
      ('Jennifer', 'Johnson', 23)

I would build up several of these queries via something like this. 我将通过类似这样的方式建立一些这样的查询。 (I'm writing this off of the top of my head, so there's almost certainly a bug in here. That's not important -- it's meant to illustrate my point): (我是在脑海中写这本书的,所以这里肯定有一个错误。这并不重要-只是为了说明我的观点):

var queriesNeeded = people.Count / 250;

for (int i = 0; i < queriesNeeded; i++)
{
   var records = people.Skip(i * 250).Take(250);
   BuildAndExecuteQuery(records);
}

// Handle anything left over
var leftoverRecords = people.Skip(queriesNeeded * 250);
BuildAndExecuteQuery(leftoverRecords);

However, while I know how to paramaterize a basic single-line INSERT, I don't know how to go about paramaterizing an INSERT that can have an unknown number of variables in it. 但是,尽管我知道如何对基本的单行INSERT进行参数化,但我不知道如何对其中可以包含未知数量变量的INSERT进行参数化。 Is there a way to do that? 有没有办法做到这一点? How would I go about creating my SqlCommand for this? 我将如何为此创建我的SqlCommand

Option 2: SqlBulkCopy Class 选项2:SqlBulkCopy类

Or... I could go the SqlBulkCopy route. 或者...我可以走SqlBulkCopy路线。 The only problem I have here is that I can't find anything that indicates how to parameterize this, or if I even need to. 我在这里唯一的问题是,我找不到任何指示如何参数化此参数的东西,或者甚至不需要。 Do I need to worry about escaping my data, or handling parameters at all here? 我是否需要担心转义数据或在此处处理参数?

Not much experience with in-memory Bulk Copy here, but I can certainly answer this: 在此处进行内存内批量复制的经验不多,但是我可以肯定地回答:

I don't know how to go about paramaterizing an INSERT that can have an unknown number of variables in it. 我不知道如何对可能具有未知数量变量的INSERT进行参数化。

You do it by adding parameters in the same loop that you use to add them to the query. 通过在将参数添加到查询的同一循环中添加参数来完成此操作。

To extend your psuedo-code, you would replace this: 要扩展您的伪代码,您可以替换为:

   BuildAndExecuteQuery(records);

With something like this: 用这样的东西:

foreach record in records
{
   Add parameter(s) to the query
   Add the same parameter(s) to the command's Parameters collection
}
ExecuteQuery

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

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