Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you perform single inserts, you can check for bad relatives at the time of the insert. You can than copy the bad data to a new table.</p> <p>In this example I have defined a typed dataset called "dsBadRowTest" Which contains a table called "ParentTable" with the column "PrimaryKeyColumn". It is the parent table of another table called "ChildTable" on it's "ForeignKeyColumn". I define a relational dataset and then attempt to add invalid children. For each invalid child, I store the column information in a separate table variable.</p> <pre><code>//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 &lt;= 5; i++) { dsRelated.ParentTable.AddParentTableRow(i); } //attempt to fill our child table, with invalid children for (Int32 i = 1; i &lt;= 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 &gt; 0) { //Uh oh, we had some bad inserts //...do something with the list of errors... } </code></pre> <p>You can keep the relations in place and check for specific exceptions as they are throw.</p> <p>(Here is the typed dataset I used for this sample... (dsBadRowTest.xsd)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;xs:annotation&gt; &lt;xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource"&gt; &lt;DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource"&gt; &lt;Connections /&gt; &lt;Tables /&gt; &lt;Sources /&gt; &lt;/DataSource&gt; &lt;/xs:appinfo&gt; &lt;/xs:annotation&gt; &lt;xs:element name="dsBadRowTest" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="dsBadRowTest" msprop:Generator_UserDSName="dsBadRowTest"&gt; &lt;xs:complexType&gt; &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt; &lt;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"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="PrimaryKeyColumn" msprop:Generator_ColumnVarNameInTable="columnPrimaryKeyColumn" msprop:Generator_ColumnPropNameInRow="PrimaryKeyColumn" msprop:Generator_ColumnPropNameInTable="PrimaryKeyColumnColumn" msprop:Generator_UserColumnName="PrimaryKeyColumn" type="xs:int" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;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"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;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" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;/xs:choice&gt; &lt;/xs:complexType&gt; &lt;xs:unique name="Constraint1" msdata:PrimaryKey="true"&gt; &lt;xs:selector xpath=".//mstns:ParentTable" /&gt; &lt;xs:field xpath="mstns:PrimaryKeyColumn" /&gt; &lt;/xs:unique&gt; &lt;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"&gt; &lt;xs:selector xpath=".//mstns:ChildTable" /&gt; &lt;xs:field xpath="mstns:ForeignKeyColumn" /&gt; &lt;/xs:keyref&gt; &lt;/xs:element&gt; &lt;/xs:schema&gt; </code></pre> <p>I hope this helps you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload