Update:
Http://blog.csdn.net/chengking/archive/2005/10/03/494717.aspx)
(1) Description
When dataset contains both primary and subtables (primary key tables and foreign key tables), sometimes the relationship constraints are too strict:
For example, relational database integrity rules:
1. entity integrity. The primary key cannot be blank in the primary key table.
2. Integrity of reference. The foreign key value in the foreign key table must correspond to the primary key in the primary key table.
It is either null or a primary key value in the primary key table.
3. Custom integrity.
If the multi-Table relationship defined in the dataset table is too restrictive, use the update method directly.
When multiple tables in a dataset are submitted together, the integrity rules may not be met and an error may occur,
Cause: for example, it is set to two tables: companymain (company main table) and companyson (company sub-table)
Companymain (company main table) stores the basic information of a company, companyson (company sub-table)
It mainly stores some customer information of the company, and the (ID) in the company's main table and the (belongid) in the sub-table)
A company corresponds to multiple customers, that is, the ID: belongid = 1: n relationship.
The ID of the primary table is the automatically generated number.
So:
On the newly added company interface, the system does not comply with the "database integrity rules" to update to the database.
If it updates the sub-table first and then updates the master table, an error may be reported because the company information of the master table has not been inserted.
If no company ID: ID is generated for the table in the database, no corresponding belongid is generated when the sub-table is updated.
If the "foreign key in the foreign key table is not null" Integrity rule is set, an exception is thrown.
This is only a possibility of errors, and there are more possibilities for errors, which are more likely to occur in distributed design.
(2) Solutions
Generally, the following rules are followed to avoid a large number of errors.
1 rule. Before dataset is updated, split and submit according to <Table> and <Table rowstate attribute>.
I. Splitting and submitting by <Table> means:
Instead of submitting tables in dataset together, one table is submitted at a time for multiple submissions.
Ii. Split and submit by <Table rowstate attribute> means:
Split a single table in dataset Based on the rowstate attribute and then perform rowstate
The same commit is performed multiple times.
2. Rules.
On the basis of rule 1, update the status to <New> and <modify> before updating <deleted>
That is, first update the datarowstate values: added and modified, and then update: deleted.
3. Rules
Based on rule 1 and rule 2, if datarowstate is added and modified
Master table, and then update the sub-table.
Based on rule 1 and rule 2, if datarowstate is deleted, the sub-table is updated first, and then the master table is updated.
(3) summarize the above three rules as follows:
1. Split the tables in dataset, group the table records based on rowstate, and store them in different datasets.
// The data is stored in dataset because update accepts dataset parameters, and WebService only supports
// Dataset for serialization
Sample Code:
Set the dataset to be updated as dscompany (which includes two tables, the primary table and the sub-table respectively, and stores
Data to be updated)
// Dtcompanymain stores the company's master table information and dtcompanyson stores the company's sub-table information
Datatable dtcompanymail = Ds. Tables ["dtcompanymain"]. Clone (); // separate the data in the master table.
And store another object
Datatable dtcompanyson = Ds. talbes ["dtcompanyson"]. Clone (); // separate sub-table data
And store another object
Dataset dsadded = new dataset (); // stores new data in the master table
Dataset dsmidified = new dataset (); // stores Row Records edited in the master table
Dataset dsdeleted = new dataset (); // stores row records deleted from the master table
Dsadded = dtcompanymain. getchanges (datarowstate. Added); // get the new row in the master table
Record set
Dsmidified = dtcompanymain. getchanges (datarowstate. Modified); // obtain the data in the master table
Set of Row Records
Dsdeleted = dtcompanymain. getchanges (datarowstate. Deleted); // obtain
Row record set
2. Update the records in the datarowstate of the master table: added and modified.
Sqldataadapter. Update (dsadded, "dtcompanymain"); // update the added record set to the database
Sqldataadapter. Update (dsmodified, "dtcompanymain"); // update the modified record set to data
Library
3. Update the records in the datarowstate of the sub-table: added and modified.
...... // The code is omitted, which is similar to the update of the two main tables.
4. Update the subtable datarowstate to a record in the deleted state.
...... // The code is omitted, which is similar to the following 5
5. Update the record whose status of datarowstate is deleted.
Sqldataadapter. Update (dsdeleted, "dtcompanymain"); // update the modified record set to the database