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.
----
Datarow Replication
If you directly use requirdt. Rows. Add (ROW), an error message "this row already belongs to a table" is displayed during running!
As for the reason, the dataset and datatable analogy should not show error prompts in general sense, but carefully
I think it is because of the design of the datatable architecture that datacolumn defines Based on the Relational Tables in relational databases.
The overall structure of the datatable, and the specific datarow is a record that conforms to this architecture! Therefore, only rows with this architecture can be added
To tables with the same architecture! At the same time, requiredt. Rows. Add () is used to capture system. argumentexception (the row
Ther belongs to another table or ready belongs to this table.) I think its implementation also limits that it cannot be implemented through this method!
In view of the above
1. atatable requiredt = sourcedt. Clone ();
Requiredt. importrow (ROW );
2. See requiredt. Rows. Add (row. itemarray) on the Internet)
But I wroteCodeIt cannot be implemented. The function is creates a row using specified values and adds it to
System. Data. datarowcollection. Therefore, you also need to copy the structure of the source data table to succeed!
3. The following methods can also be implemented, but you must copy the table structure first.
Objecttable. beginloaddata ();
Myrow = objecttable. loaddatarow (newrow, true );
Objecttable. endloaddata ();
_______________________________________________________________________________________________
conclusion:
when we want to copy a 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.