Click here to Skip to main content
15,302,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a two datatable(dt1 and dt2)
column 1 to 5 in dt1
and i want to copy few records from first datatable to second datatable(copy column 1,2,3 in dt2)

how?

thanks


How I copy data table specifiy columns to another Datat able?


In C# , I m using one datatable concept, In that datatable had 7 columns,
Now I want to copy first 5 Coulmns for first New Datatable How do I this ?

Source Table Name - Days

Date | Day1 | Slot 1 | Day 2 | Slot 2 | Day 3 | Slot 3

9:00 AM | Invite | A,0 | Invite | B,0 | Invite | C,0
9:20 AM | Invite | A,1 | Invite | B,1 | Invite | C,1


New Datable Name : dtInternational

Date | Day1 | Slot 1 | Day 2 | Slot 2

9:00 AM | Invite | A,0 | Invite | B,0
9:20 AM | Invite | A,1 | Invite | B,1
Posted
Updated 3-Oct-12 21:26pm
v2

Hi,

Use the below query

SQL
INSERT INTO dt2 VALUES as
                 SELECT col1,col2,col3 FROM dt1


Please make sure that data type and length of subsequent columns are same.

You can also specify column name in table 2 on which column you want to insert data.

as below

SQL
INSERT INTO dt2<columns name in which you want to insert the data.> VALUES as
                 SELECT col1,col2,col3 FROM dt1
   
v2
i friend,

Apply a select query for the data table, you will get the selected rows from one datatable to another datatable.

http://www.dotnetperls.com/datatable-select[^]

http://msdn.microsoft.com/en-us/library/det4aw50.aspx[^]
   
hi ,
With below query you can write the column names also ans where condition also.

DataRow[] rows = datatable.Select('TOP 5');
   
then what is your problem this is not proper question .u did not explain your tables structure and condition .
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900