DataSet, DataTable, and DataRow copying DataSet objects supports 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 );
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 aDataColumn 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. You can copy the data of 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.