DataSet..::. Merge Method (DataSet)

Source: Internet
Author: User
Tags one table

Merges the specified dataset and its schema into the current dataset.

namespaces: System.Data
Assembly: System.Data (in System.Data.dll)

C#

Public
 void
 Merge (
DataSet DataSet
)
ParametersDataSet Type:
System.Data.. :: . DataSet

The dataset whose data and schema will be merged. Often
Exception condition
ConstraintException

One or more constraints cannot be enabled.

ArgumentNullException

The DataSet is null nothing nullptr a null reference (Nothing in Visual Basic).

Note
The merge method is used to consolidate two DataSet objects that are roughly similar to the schema. Merging on client applications is typically used to merge the most recent changes in the data source into the existing

DataSet. This enables the client application to have a flush with the latest data from the data source
DataSet.


Typically called at the end of a series of procedures
The Merge method, which involves verifying changes, eliminating errors, updating the data source with changes, and finally refreshing the existing
DataSet.


In a client application, there is usually a button that the user can click to collect and validate the changed data, and then send it back to the middle-tier component. In this case, the first call to the
GetChanges method. This method returns another one that is optimized for validation and merging.
DataSet. A second
The DataSet object contains only changed
DataTable and
DataRow object, resulting in initial
A subset of the DataSet. This subset is usually small, so it can be passed back to the middle tier component more efficiently. The middle-tier component then uses the changes to update the initial data source through stored procedures. The middle tier can then send back a new
DataSet that contains the initial data and the latest data from the data source (by running the initial query again), or it can send back a subset of all changes made to it from the data source. (for example, if your data source automatically creates unique primary key values, you can propagate these values back to the client application.) In either case, you can use the
The Merge method will return the
The DataSet is merged back into the client application's initial
DataSet.


When you call
Merge method, the schema may have changed, so the two
The schema of the DataSet object is compared. For example, in the case of an enterprise to the enterprise, a new column may have been added to the XML schema by an automated process. If the source
The DataSet contains missing schema elements in the target (added
DataColumn object), you can add the schema element to the target by setting the MissingSchemaAction parameter to MissingSchemaAction.Add. In this case, the merged
The DataSet contains the schemas and data that you have added.


Merge the data after the schema is merged.


When the new source
When the DataSet is merged into the target,
Any source row with the DataRowState value of unchanged, Modified, or Deleted will match the target row with the same primary key value. The source row of the DataRowState value is Added matches those new target rows that have the same primary key value as the Xinyuan row.


Constraints are disabled during the merge process. If you cannot enable any constraints at the end of the merge, the constraint is generated at the same time
ConstraintException and retains the merged data. In this case,
The EnforceConstraints property will be set to false and all invalid rows will be marked as errors. In an attempt to
These errors must be eliminated before the EnforceConstraints property is reset to True. example, the following example is for
DataSet uses

GetChanges, Update, and
The Merge method.



C#




private void Demonstratemerge ()
{
Create a DataSet with one table, two columns, and three rows.
The DataSet DataSet = new DataSet ("DataSet");
DataTable table = new DataTable ("Items");
DataColumn idcolumn = new DataColumn ("id",
Type.GetType ("System.Int32"));
Idcolumn.autoincrement=true;
DataColumn itemcolumn = new DataColumn ("Item",
Type.GetType ("System.Int32"));

DataColumn array to set primary key.
Datacolumn[] keycolumn= new datacolumn[1];
DataRow Row;

Create variable for temporary DataSet.
DataSet Changedataset;

ADD columns to table, and table to DataSet.
Table. Columns.Add (Idcolumn);
Table. Columns.Add (Itemcolumn);
DATASET.TABLES.ADD (table);

Set primary key column.
Keycolumn[0]= Idcolumn;
Table. Primarykey=keycolumn;

ADD ten rows.
for (int i = 0; I <10;i++)
{
Row=table. NewRow ();
row["Item"]= i;
Table. Rows.Add (row);
}

Accept changes.
Dataset.acceptchanges ();
Printvalues (DataSet, "Original values");

Change two row values.
Table. rows[0]["Item"]= 50;
Table. rows[1]["Item"]= 111;

ADD one row.
Row=table. NewRow ();
row["Item"]=74;
Table. Rows.Add (row);

Insert code for error checking. Set one row in error.
Table. ROWS[1]. Rowerror= "Over 100";
Printvalues (DataSet, "Modified and New Values");
If The table has changes or errors, create a subset DataSet.
if (Dataset.haschanges (datarowstate.modified |
datarowstate.added) & Dataset.haserrors)
{
Use GetChanges to extract subset.
Changedataset = DataSet.GetChanges (
datarowstate.modified| datarowstate.added);
Printvalues (Changedataset, "subset Values");
Insert code to reconcile errors. In the case reject changes.
foreach (DataTable changetable in Changedataset.tables)
{
if (changetable.haserrors)
{
foreach (DataRow changerow in Changetable.rows)
{
Console.WriteLine (changerow["Item"]);
if (int) changerow["Item",
DataRowVersion.Current]> 100)
{
Changerow.rejectchanges ();
Changerow.clearerrors ();
}
}
}
}
Printvalues (Changedataset, "reconciled subset Values");
Merge changes back to the.
DataSet.Merge (Changedataset);
Printvalues (DataSet, "merged Values");
}
}

private void Printvalues (DataSet DataSet, string label)
{
Console.WriteLine ("N" + label);
foreach (DataTable table in Dataset.tables)
{
Console.WriteLine ("TableName:" + table). TableName);
foreach (DataRow row in table). Rows)
{
foreach (DataColumn column in table. Columns)
{
Console.Write ("/table" + row[column]);
}
Console.WriteLine ();
}
}
}



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.