什么是从C#批量数据库插入的最佳方法? [英] What’s the best way to bulk database inserts from c#?
问题描述
我如何/什么是应该做批量数据库插入的最佳方式?
How do I/what’s the best way to do bulk database inserts?
在C#中,我遍历集合并要求集合中的每个项目插入存储过程。
In C#, I am iterating over a collection and calling an insert stored procedure for each item in the collection.
我如何发送的所有数据在一个数据库调用?
How do I send all the data in one database call?
例如。说我有一个人名单(列表<
;人&GT)含10个项目。我目前调用InsertPerson存储过程10次。我想这减少到1来电。
E.g. say I have a person list (List<Person>
) containing 10 items. I am currently calling the InsertPerson stored proc 10 times. I would like to reduce this to 1 call.
我使用MS SQL Server 2005中。
I am using MS SQL Server 2005.
推荐答案
好了,10个项目是不是我叫大头,但对于更大的套什么, SqlBulkCopy的
是朋友。所有你需要做的就是给它是数据表
或的IDataReader
(我的preferred选项,COS我喜欢流式API)。我做了类似的事情<一href=\"http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/91c7a20056ffe8e1\">here (你可以忽略XML方 - 只要子类SimpleDataReader)。
Well, 10 items isn't what I call bulk, but for larger sets, SqlBulkCopy
is your friend. All you need to do is feed it either a DataTable
or an IDataReader
(my preferred option, 'cos I like streaming APIs). I did something similar here (you can ignore the xml side - just subclass the SimpleDataReader).
这篇关于什么是从C#批量数据库插入的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!