Dataset, datatable and datarow replication method

Source: Internet
Author: User
Dataset, datatable and datarow replication method

Dataset objects support ADO. the core object of the net disconnected and distributed data solution, which is widely used. we often need to use the data, such as getting data from a able, copying data from another Abe, or data from datarow. However, only the replication of dataset and datatable supports deep replication, that is to say, the structure of the element can be copied, and the data of the element can be copied. The datatdatarow has no relevant replication method. The following describes the replication of these data elements.

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 );

 

 

Analyzed the cause, becauseDatarow ableBoth are called by reference. Therefore, if a row is in one table, it cannot be added to another table. 

 

Specific Method:

 

1Importrow 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. LoopDatatableEach column

Datarow adatarow = objecttable. newrow ();

Foreach(Datacolumn adatacolumnInSourcetable. 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 ();

This method is complex. We recommend that you do not use it if you simply copy data from existing rows to add new rows. For more information about the usage, see the SDK document.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1501703

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.