批量插入问题 [英] Bulk insert questions

查看:143
本文介绍了批量插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在客户端的CSV文件,我想开发一个C#应用程序批量数据插入到数据库中,以最小的日志输出表。我很困惑,如果我使用ADO.NET在客户端调用在数据库服务器中的存储过程。什么样的code需要开发的客户端,需要在服务器端的存储过程的形式来实现什么样的code?

I have a CSV file at the client side, and I want to develop a C# application to bulk insert the data into a table of a database to minimal log output. I am confused about if I use ADO.NET at the client side to call stored procedures in the database server. What kind of code needs to develop at the client side and what kind of code needs to be implemented at the server side in the form of stored procedures?

不过,我并没有发现来自谷歌的任何样本。什么是一些准备使用的样本? : - )

But I did not find any samples from Google. What are some ready to use samples? :-)

编辑:一些详细信息:

我有很多数据在客户端,我想导入到数据库中,但我不希望所有的许多事务日志的开销。出于安全原因,我想开发在服务器端存储过程和客户端(ADO.NET)调用。我想知道达到这样的目标。需要在服务器端进行开发的存储过程和什么样的T-SQL如何调用/在客户端有效地填充数据?

I have a lot of data at the client side and I want to import to the database, but I do not want the overhead of all the many transaction logs. For security reasons, I want to develop a stored procedure at the server side and call from client side (ADO.NET). I want to know to achieve such goal. What kind of T-SQL needs to be developed in stored procedures at the server side and how to call/fill data efficiently at the client side?

如果事情还不清楚,请随时让我知道。

If anything is still unclear, please feel free to let me know.

推荐答案

您可以挂钩 CsvReader 到SqlBulkCopy的,由它来完成的工作非常漂亮......类似的信息(未测试):

You can hook CsvReader to SqlBulkCopy, which does the job very nicely... something like (untested):

using (CsvReader reader = new CsvReader(path))
using (SqlBulkCopy bcp = new SqlBulkCopy(CONNECTION_STRING))
{
    bcp.DestinationTableName = "SomeTable";
    bcp.WriteToServer(reader);
}

修改,你通常会做批量插入到上演表,然后使用常规存储过程将数据移动到的真正表。

edit you would typically do the bulk-insert into a staging table, and then use a regular stored procedure to move the data into the real table.

这篇关于批量插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆