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.
精彩评论