Copy From One datatable to another

Posted by Santosh.Choudhury under ASP.NET on 1/11/2014 | Points: 10 | Views : 648 | Status : [Member] | Replies : 1
I have two datatable i want to copy from first datatable to another first 10 rows .how to do?columns are dynamic in that data table so we cant mention the column name.2nd datatable is empty




Responses

Posted by: Sheonarayan on: 1/11/2014 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
You can use BulkCopy from ADO.NET.

Below are the steps you need to perform (provided you have same data structure in both database). Select * record from the source database table (SELECT * FROM myTable) and keep it into DataTable object (Use simple ADO.NET objects).

Use below code, where sourceDataTable is the DataTable that has source database records.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName =
"dbo.MatchingColumnsTableName";

try
{
bulkCopy.WriteToServer(sourceDataTable);
}
catch
{
throw;
}
}

For more visit http://msdn.microsoft.com/en-us/library/ex21zs8x(v=vs.110).aspx.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Santosh.Choudhury, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response