C # copy a datarow file from a bag

Source: Internet
Author: User
Two tables A and B have the same structure. Now we need to copy the branch in the middle of Table A to table B.
If datatableb. Rows. Add (datatablea. Rows [0]) is directly used, an error such as "row already belongs to table a" is reported.
So we can use another method.
Method 1: Use the datatable. importrow () method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. importrow (drval );
}
}

Method 2: Use the datatable. Rows. Add (Params object [] values) method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. Rows. Add (drval. itemarray );
}
} Chen Kuan Tian has two tables A and B with the same structure. Now we need to copy the branch in the middle of Table A to table B.
If datatableb. Rows. Add (datatablea. Rows [0]) is directly used, an error such as "row already belongs to table a" is reported.
So we can use another method.
Method 1: Use the datatable. importrow () method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. importrow (drval );
}
}

Method 2: Use the datatable. Rows. Add (Params object [] values) method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. Rows. Add (drval. itemarray );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.