从 CSV 批量插入到数据库 (C#) -> Web 应用程序中的最大行数?
Web 应用程序 - C#、.Net、SQL 2k5。 我最近在另一个应用程序上使用了bulkinsert,我想尝试一下。
我将收到一个包含 1000 行的 CSV 文件,这很可能会在数据库中添加 500 000(即五十万)条记录。 我还不知道这笔巨额资金是否会取得良好的效果。我担心它会超时。
我还没有进行任何测试,但我很确定它最终会超时。
有没有办法让它不超时(我不知道......将bulkinsert分成1000块:D)或者我应该尝试做一些像BCP这样的事情,用SQL作业......
Web application - C#, .Net, SQL 2k5.
I recently used bulkinsert on an other application and I thought I would like to give it a try.
I am going to receive a CSV file with 1000 rows, which will most likely add 500 000 (that is five hundred thousand) records in the database.
I don't have any idea yet about this huge amount if it's going to work out well. I am afraid that it will time out.
I didn't do any testing yet, but I am pretty sure it would time out eventually.
Is there a way to make it not time out (I don't know ... split the bulkinsert into 1000 pieces :D) or I should try to do something like BCP, with a SQL job ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近使用 C# 和 SqlBulkCopy 类开发了类似的批量插入功能。为了避免页面超时,我使用 ThreadPool(ThreadPool.QueueUserWorkItem 方法)进行异步处理。使用新的连接对象将上传状态添加到日志表中,以避免日志的事务回滚。然后使用“上传历史记录”页面在网站中报告此状态。
最好的解决方案是创建一个使用 BulkInsert 命令的过程。这是该过程的示例代码:
I recently developed smiliar bulkinsert functionality using c# and SqlBulkCopy class. To avoid page timeout I did asynchornous processing using ThreadPool (ThreadPool.QueueUserWorkItem method). The upload status is added to a log table using a new connection object to avoid transaction rollback for logs. This status is then reported in the website using a Upload History page.
The best solution would be to create a procedure which uses BulkInsert command. Here is a sample code for the proc:
实际上,拆分插入似乎是个好主意。
我会使用更大的块,比如 5000 或更多。
Actually, splitting insertion seems a good idea.
I would use bigger chunks, say 5000 or maybe more.
我建议您不要尝试在单个 Web 服务调用中处理该文件。
-或者-
如果您创建一个 WF/WCF 工作流服务,您可以一次性完成这两项任务,您可以将其直接托管在 IIS 中,就像 Web 服务一样,但它可以执行多个操作。
I suggest you don't try to process the file in a single web service call.
-or-
You may be able to do both in one swoop if you create a WF/WCF Workflow Service, which you can host directly in IIS, like a web service, but it can perform multiple operations.