[英]What is the fastest way to insert record in SQL Server from C#
I have 5 tables, for each table i want to delete data and insert 100 records. 我有5个表,我想为每个表删除数据并插入100条记录。 all operation should happen in single transaction.
所有操作应在一次交易中进行。
In real terms, 100 records is not much; 实际上,100条记录并不多; if performance is your aim, you could try using
SqlBulkCopy
- this uses raw TDS, and supports transactions via the optional constructor argument . 如果性能是您的目标,则可以尝试使用
SqlBulkCopy
这使用原始TDS,并通过可选的构造函数参数支持事务。 In terms of how to feed data in: SqlBulkCopy
accepts either DataTable
or a data reader; 关于如何输入数据:
SqlBulkCopy
接受DataTable
或数据读取器; "FastMember" allows you to treat a List<T>
or similar as a data-reader, if that helps (see example at the bottom of this page ). 如果有帮助,“ FastMember”允许您将
List<T>
或类似的东西视为数据读取器(请参阅本页底部的示例)。 However, you should be careful to actually time that against doing the same thing with regular parameterized TSQL operations, as for a number like 100 it isn't guaranteed that SqlBulkCopy
would be faster. 但是,您应该小心地计时,以免对常规的参数化TSQL操作执行相同的操作,因为对于100这样的数字,不能保证
SqlBulkCopy
会更快。
An alternative to SqlBulkCopy
would be "table valued parameters"; SqlBulkCopy
的替代方法是“表值参数”。 this requires more config, as the user type needs to be defined on the server etc, but; 这需要更多的配置,因为需要在服务器等上定义用户类型,但是; it can work.
它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.