C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Copying Data from one DataTable to Another


Posted Date: 03 Nov 2006    Resource Type: Articles    Category: .NET Framework
Author: Sivakesh RamanMember Level: Bronze    
Rating: 1 out of 5Points: 10



Introduction


I recently had to copy large data from one dataset to another.

To do so, I decided to use DataTable objects. If you go through the rows of a DataTable and direct copy rows from one DataTable, you will get an exception saying "Row is already being used by another table".

The simplest way is to clone an existing DataTable, loop through all rows of source DataTable and copy data from column by column and add row to the destination DataTable. The following code does the same:


For Each dr As DataRow In sourceTable.Rows
r = destinationTable.NewRow
r("Name") = dr("Name")
r("City") = dr("City")
r("Cost") = dr("Cost")
destinationTable.Rows.Add(r)
Next


DataTable.ImportRow Method



The second method is using DataTable.ImportRow method. The ImportRow method of DataTable copies a row into a DataTable with all of the properties and data of the row. It actually calls NewRow method on destination DataTable with current table schema and sets DataRowState to Added. The following code does the same as we did in the previous code snippet.


For Each dr As DataRow In sourceTable.Rows
destinationTable.ImportRow(dr)
Next


Performance Check



Here are the numbers for both methods:

Number of redords Time in Milliseconds
Direct Copy DataTable.ImportRow
1,000 9.8 9.8
10,000 895.78 420.56
100,000 7089.34 3409.42

When I tried to copy over 10 million rows, DataTable.ImportRow method took 56 seconds but I got memory exception using direct copy method.

Summary



When it comes to copying bulk of data from one DataTable to another, DataTable.ImportRow method comes handy. This article compares both direct copy and ImportRow method and we saw ImportRow method may reduce the processing time when dealing with large data.




Responses

Author: Gaurav    27 Apr 2007Member Level: Bronze   Points : 0
I was really looking for the same method!


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Design patterns
Previous Resource: Type-Safe code. What and why?
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use