Если вы выполняете одиночные вставки, вы можете проверить наличие плохих родственников во время вставки.Вы можете скопировать неверные данные в новую таблицу.
В этом примере я определил типизированный набор данных с именем "dsBadRowTest", который содержит таблицу с именем "ParentTable" и столбцом "PrimaryKeyColumn".Это родительская таблица другой таблицы с именем «ChildTable» в «ForeignKeyColumn».Я определяю реляционный набор данных, а затем пытаюсь добавить недопустимых потомков.Для каждого недопустимого дочернего элемента я сохраняю информацию столбца в отдельной табличной переменной.
//Our relational dataset...
dsBadRowTest dsRelated = new dsBadRowTest();
//The error table will be a non-relational version
dsBadRowTest.ChildTableDataTable dtErrors = new dsBadRowTest.ChildTableDataTable();
//Add an extra column the error table for extra info
dtErrors.Columns.Add("ErrorMessage");
//Fill our parent table
for (Int32 i = 1; i <= 5; i++) {
dsRelated.ParentTable.AddParentTableRow(i);
}
//attempt to fill our child table, with invalid children
for (Int32 i = 1; i <= 10; i++) {
dsBadRowTest.ChildTableRow drNewChild = dsRelated.ChildTable.NewChildTableRow;
drNewChild.ForeignKeyColumn = i;
try {
dsRelated.ChildTable.AddChildTableRow(drNewChild);
} catch (Data.InvalidConstraintException ex) {
//Problem adding...Copy the row for the error table
dsBadRowTest.ChildTableRow drError = dtErrors.NewChildTableRow;
foreach (System.Data.DataColumn dc in drNewChild.Table.Columns) {
drError(dc.ColumnName) = drNewChild(dc);
}
//Our non-typed extra column will contain the error message
drError("ErrorMessage") = ex.Message;
dtErrors.AddChildTableRow(drError);
}
}
if (dtErrors.Rows.Count > 0) {
//Uh oh, we had some bad inserts
//...do something with the list of errors...
}
Вы можете сохранить отношения на месте и проверять наличие конкретных исключений по мере их выброса.
(Здесьтипизированный набор данных, который я использовал для этого образца ... (dsBadRowTest.xsd)
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="dsBadRowTest" targetNamespace="http://tempuri.org/dsBadRowTest.xsd" xmlns:mstns="http://tempuri.org/dsBadRowTest.xsd" xmlns="http://tempuri.org/dsBadRowTest.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections />
<Tables />
<Sources />
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="dsBadRowTest" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="dsBadRowTest" msprop:Generator_UserDSName="dsBadRowTest">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ParentTable" msprop:Generator_TableClassName="ParentTableDataTable" msprop:Generator_TableVarName="tableParentTable" msprop:Generator_TablePropName="ParentTable" msprop:Generator_RowDeletingName="ParentTableRowDeleting" msprop:Generator_UserTableName="ParentTable" msprop:Generator_RowChangingName="ParentTableRowChanging" msprop:Generator_RowEvHandlerName="ParentTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ParentTableRowDeleted" msprop:Generator_RowEvArgName="ParentTableRowChangeEvent" msprop:Generator_RowChangedName="ParentTableRowChanged" msprop:Generator_RowClassName="ParentTableRow">
<xs:complexType>
<xs:sequence>
<xs:element name="PrimaryKeyColumn" msprop:Generator_ColumnVarNameInTable="columnPrimaryKeyColumn" msprop:Generator_ColumnPropNameInRow="PrimaryKeyColumn" msprop:Generator_ColumnPropNameInTable="PrimaryKeyColumnColumn" msprop:Generator_UserColumnName="PrimaryKeyColumn" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ChildTable" msprop:Generator_TableClassName="ChildTableDataTable" msprop:Generator_TableVarName="tableChildTable" msprop:Generator_TablePropName="ChildTable" msprop:Generator_RowDeletingName="ChildTableRowDeleting" msprop:Generator_UserTableName="ChildTable" msprop:Generator_RowChangingName="ChildTableRowChanging" msprop:Generator_RowEvHandlerName="ChildTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ChildTableRowDeleted" msprop:Generator_RowEvArgName="ChildTableRowChangeEvent" msprop:Generator_RowChangedName="ChildTableRowChanged" msprop:Generator_RowClassName="ChildTableRow">
<xs:complexType>
<xs:sequence>
<xs:element name="ForeignKeyColumn" msprop:Generator_ColumnVarNameInTable="columnForeignKeyColumn" msprop:Generator_ColumnPropNameInRow="ForeignKeyColumn" msprop:Generator_ColumnPropNameInTable="ForeignKeyColumnColumn" msprop:Generator_UserColumnName="ForeignKeyColumn" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:ParentTable" />
<xs:field xpath="mstns:PrimaryKeyColumn" />
</xs:unique>
<xs:keyref name="FK_ParentTable_ChildTable" refer="Constraint1" msprop:rel_Generator_UserChildTable="ChildTable" msprop:rel_Generator_ChildPropName="GetChildTableRows" msprop:rel_Generator_UserParentTable="ParentTable" msprop:rel_Generator_UserRelationName="FK_ParentTable_ChildTable" msprop:rel_Generator_RelationVarName="relationFK_ParentTable_ChildTable" msprop:rel_Generator_ParentPropName="ParentTableRow" msdata:UpdateRule="None" msdata:DeleteRule="None">
<xs:selector xpath=".//mstns:ChildTable" />
<xs:field xpath="mstns:ForeignKeyColumn" />
</xs:keyref>
</xs:element>
</xs:schema>
Надеюсь, это вам поможет.