简体   繁体   English

从C#在SQL Server中插入记录的最快方法是什么

[英]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.

相关问题 在 SQL Server(C# 客户端)中批量插入大量数据的最快方法是什么 - What's the fastest way to bulk insert a lot of data in SQL Server (C# client) 使用C#在SQL Server中保存数据的最快方法是什么? - What is the fastest way to save data in SQL Server using C#? 在C#中通过ID在数据库中查找记录的最快方法是什么? - what is the fastest way to find a record by id in a database in c#? 使用C#在SQL Server上的临时表中插入3万行的最快方法 - Fastest way to insert 30 thousand rows in a temp table on SQL Server with C# 将大量记录插入SQL Server数据库的最快方法是什么? - What is the fastest way to insert a large amount of records into a SQL Server DB? C#将数据插入SQL数据库的最快方法 - C# fastest way to insert data into SQL database 将从文本文件解析的250万行数据插入Sql Server的最快方法是什么 - What would be the fastest way to insert 2.5 million rows of data parsed from a text file, into Sql server 从C#SQLCLR存储过程批量插入SQL表的最快方法 - Fastest way to bulk insert in SQL Table from C# SQLCLR Stored Procedure c# - 将DataReader中的值插入DataGridView的最快方法 - c# - fastest way to insert value from DataReader into DataGridView 从数据库SQLite C#服务堆栈读取SQL数据(数百万条记录)的最快方法是什么 - What is the fastest way to read the SQL Data (Millions of records) from database SQLite C# Service Stack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM