Home > Backend Development > C#.Net Tutorial > DataSet, DataTable and DataRow copy methods

DataSet, DataTable and DataRow copy methods

巴扎黑
Release: 2016-12-20 09:30:17
Original
1292 people have browsed it

The DataSet object is the core object that supports ADO.NET's disconnected and distributed data solution. It has a wide range of uses. We often need to use the data in it, such as obtaining data from a DataTable or copying data from another DataTabe. DataRow data, but only the copying of DataSet and DataTable supports deep copying, which means that not only the structure of the element can be copied, but also the data of the element can be copied. However, DatatDataRow has no related copying method. The following will briefly introduce these data elements. replication issues.
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();//Shallow copy, only copy the schema
Copy DataTable
objectTable = sourceTable .Copy();//Deep copy
objectTable = sourceTable .Clone();//Shallow copy, only copy the schema
Copy DataRow
This error is often encountered in project development - "This row already belongs to another table". The statement that caused this error is as follows:
objectTable .Rows.Add(SourceDataRow);
I analyzed the reason, because DataRow DataTable is called by reference. Therefore, once a row is in one table, it cannot be added to another table.
Specific method:
1 ImportRow method: public void ImportRow( DataRow DataRow);
objectTable = sourceTable.clone();//The schema of the table must be copied first so that it has the same columns or relationships! (Foreach (dataarow orow in sourceTable) {
objecttable.importrow (orow); // Add a new line to objecttable, and copy the value of the source of Source, requires the structure of the table!
}
______________________________________________________________________________________________
2. Loop through each column of DataTable
DataRow aDataRow = objectTable.NewRow();
foreach(DataColumn aDataColumn in sourceTable.Columns)
{
aDataRow [aDataColumn.ColumnName] = sourceTable[i ][aDataColumn. ColumnName];
}
objectTable.Rows.Add(aDataRow);
3. Custom copy
objectTable.Columns.Add ("id");//You don’t need to have the same structure, 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 there is no id column in the source table in myArry, an error will be reported!
tempRow.ItemArray = myArry; //The ItemArray property is an Object type array. You can copy the data of multiple columns by yourself according to the needs of the program!
objectTable.Rows.Add(tempRow); //This method must be called, otherwise in DataRow The data will not be displayed!
}
______________________________________________________________________________________________
4. LoadDataRow method: public DataRow LoadDataRow(Object[] values,bool fAcceptChanges);
Object[] newRow = new Object[3];
// Set the value of the object array
newRow[0] = "Hello";
newRow[1] = "World";
newRow[2] = "two";
DataRow myRow;
ObjectTable.BeginLoadData();
// Add new rows to the table
myRow = ObjectTable.LoadDataRow(newRow, true);//The flag should be set to true, indicating adding a new row
ObjectTable.EndLoadData();

This method is more complicated. If you just copy the data of the existing row to add It is recommended not to use the new line. Please refer to the sdk documentation for specific usage.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template