Data replication methods for dataset, able, and datarow In ADO. net

Source: Internet
Author: User

 

 

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

Then how should we copy the data of datarow? The following describes several methods.
_____________________________________________________________________________________________________

1Importdatarow Method: Public void importdatarow (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. importdatarow (orow); // Add a new row to objecttable and copy the value of sourcerow. The table structure must be the same!

}

_____________________________________________________________________________________________________

2Custom 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!

}
_____________________________________________________________________________________________________

3Loaddatarow 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.
___________________________________________________________________________________________________
Summary:
When we want to copy the row of a datatable to another datatable, we cannot directly use objecttable. add (soucedatarow);, or use objectrow = sourcerow to assign values, and then add. If this error occurs, the compiler will prompt you "this row already belongs to another table". This is also true for datatable, because all objects in this way are of reference type .. Net does not allow other tables to reference a row or other dataset that already belongs to the current table to reference the datatable in the current dataset. The above method must be used to copy the data first; the method and method of replication depend entirely on the needs of your program.

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.