开发者

Insert data from DataTable to database table

开发者 https://www.devze.com 2023-03-27 12:56 出处:网络
What is the easiest and most efficient way to insert data from a DataTable into a SQL Server database table?For what it\'s worth, the DataTable and the database table have matching columns.

What is the easiest and most efficient way to insert data from a DataTable into a SQL Server database table? For what it's worth, the DataTable and the database table have matching columns.

What I am doing is transferring data from one database to another (separate instances, so I will be using my application as the intermediate "receiver/sender" of data). I can easily populate a DataTable with a SqlDataAdapater.Fill() call from the source database table. But I'm trying to find the most proficient way to send that DataTable's data to the final destination database table.

Any suggestions/advice/opinions are much app开发者_运维百科reciated.

EDIT: the destination database table already exists.


You should take a look at the SqlBulkCopy class, particularly the overload of the WriteToServer method that takes a DataTable as a parameter.

If you want to be even more efficient, and you don't have the requirement to materialize the entire table into a DataSet (or, you can process the contents as you move them in a forward-only manner), then use the overload of WriteToServer that takes an IDataReader run your query using the ExecuteReader method on the SqlCommand class instead of using a SqlDataAdapter to load the entire table into memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消