使用 SQL Server 2008 和 BCP 进行快速批量插入/更新
我不是一个好的 SQL 程序员,我只有基础知识,但我听说过一些用于快速数据加载的 BCP 东西。我在互联网上搜索过,它似乎只是一个命令行实用程序,而不是可以在代码中使用的东西。
问题是,我希望能够在 SQL Server 2008 数据库中进行非常快速的插入和更新。我希望数据库中有一个函数可以接受:
- 我想要执行插入/更新操作的表的名称
- 我将向其提供数据的列的名称
- CSV 格式的数据或其他格式的数据SQL 可以快速读取
- 一个标志,指示函数应该执行插入或更新操作,
然后该函数将读取此 CSV 字符串并生成插入/更新表所需的代码。
然后,我会用 C# 编写代码来调用该函数,并向其传递表名称、列名称、序列化为 CSV 字符串的对象列表以及插入/更新标志。
正如您所看到的,这既快速又通用,适合任何处理大量数据的项目,因此是我公司框架的候选者。
我想得对吗?这是个好主意吗?我可以使用 BCP 吗?它适合所有情况吗?
如您所见,我需要一些关于此的指示...提前感谢您的帮助!
I'm not a good SQL programmer, I've got only the basics, but I've heard of some BCP thing for fast data loading. I've searched the internet and it seems to be a command-line only utility, and not something you can use in code.
The thing is, I want to be able to make very fast inserts and updates in a SQL Server 2008 database. I would like to have a function in the database that would accept:
- The name of the table I want to execute an insert/update operation against
- The names of the columns I'll be feeding data to
- The data in a CSV format or something that SQL can read stupid-fast
- A flag indicating weather the function should perform an insert or update operation
This function would then read this CSV string and genarate the necessary code for inserting/updating the table.
I would then write code in C# to call that function passing it the table name, column names, a list of objects serialized as a CSV string and the insert/update flag.
As you can see, this is intended to be both fast and generic, suitable for any project dealing with large amounts of data, and thus a candidate to my company's framework.
Am I thinking right? Is this a good idea? Can I use that BCP thing, and is it suitable to every case?
As you can see, I need some directions on this... thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C# 中,请查看 SQLBulkCopy。这就是 SSIS 在后台使用的内容。
对于 true bcp/批量插入,您需要批量管理权限,但可能不允许
In C#, look at SQLBulkCopy. It's what SSIS uses in the background.
For true bcp/BULK INSERT, you'd need bulkadmin rights which may not be allowed
您是否考虑过使用 SQL Server 集成服务 (SSIS)。它旨在完全按照您所描述的方式进行操作。它非常快。您可以基于事务插入数据。您可以将其设置为按计划运行。还有更多。
Have you considered using SQL Server Integrated Services (SSIS). It's designed to do exactly what you describe. It is very fast. You can insert data on a transactional basis. And you can set it up to run on a schedule. And much more.