Dataset, able Replication

Source: Internet
Author: User

Dataset, able Replication

Datatable sourcetable;
Datatable objecttable;
Datatdatarow sourcerow;
Datatdatarow objectrow;
Datarow temprow;
Dataset soucedataset = new dataset ();
Copy Dataset

DataSet object = soucedataset. Copy (); // deep copy
DataSet object = soucedataset. Clone (); // copying architecture only

Copy datatable

Objecttable = sourcetable. Copy (); // deep copy
Objecttable = sourcetable. Clone (); // replication-only architecture

Copy datarow

This error is often encountered during project development-"This row already belongs to another table ". The statement that causes this error is as follows:

 

Objecttable. Rows. Add (sourcedatarow );

 

 

The reason is analyzed because datarow able is called by reference. Therefore, if a row is in one table, it cannot be added to another table.

 

Specific Method:

 

1 importrow method: Public void importrow (datarow );

Objecttable = sourcetable. Clone (); // you must first copy the schema of the table to have the same columns or relationships!
Foreach (datarow orow in sourcetable)
{

Objecttable. importrow (orow); // Add a new row to objecttable and copy the value of sourcerow. The table structure must be the same!

}

2. Loop every column of the datatable

Datarow adatarow = objecttable. newrow ();

Foreach (datacolumn adatacolumnyle = "color: Blue"> In sourcetable. columns)

{

Adatarow [adatacolumn. columnname] = sourcetable [I] [adatacolumn. columnname];

}

Objecttable. Rows. Add (adatarow );

3. Custom Replication

Objecttable. Columns. Add ("ID"); // you do not need to have the same architecture. just copy the columns you need!
Object [] myarry = new object [1];
Foreach (datarow orow in sourcetable)
{

Temprow = objecttable. newrow (); // This method must be called!
Myarry [0] = orow ["ID"]; // If the ID column in the source table does not exist in myarry, an error is returned!
Temprow. itemarray = myarry; // The itemarray attribute is an array of the object type.ProgramYou can copy data from multiple columns as needed!
Objecttable. Rows. Add (temprow); // This method must be called; otherwise, data in datarow cannot be displayed!

}
_____________________________________________________________________________________________________

4. loaddatarow method: Public datarow loaddatarow (object [] values, bool facceptchanges );

Object [] newrow = new object [3];
// Set the object array value
Newrow [0] = "hello ";
Newrow [1] = "world ";
Newrow [2] = "two ";
Datarow myrow;
Objecttable. beginloaddata ();
// Add a new row to the table
Myrow = objecttable. loaddatarow (newrow, true); // The flag must be set to true to add a new row.
Objecttable. endloaddata ();

 

For your security, please only open the URL with reliable source

Cancel website opening

From: http://hi.baidu.com/zhendong7799/blog/item/24a34594d774ae047bf480a7.html

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.