Home All Groups Group Topic Archive Search About

DataAdapter & AddWithKey

Author
22 Oct 2006 7:07 PM
Micus
[Win XP Pro, SQL Server 2000, VS 2005 Pro, C++/CLI]

Hi All,

    I'm having a problem with using the SqlDataAdapter->Fill() method. The
following is a simplified version of the error

TableA
aColA (Primary Key, Identity)
aColB, aColC, etc...

TableB
bColA (Primary Key, Identity)
bColB, bColC, etc...

DataSet ^ds = gcnew DataSet();

SqlDataAdapter ^daA = gcnew SqlDataAdapter();
daA->MissingSchemaAction = MissingSchemaAction::AddWithKey;
SqlDataAdapter  ^daB = gcnew SqlDataAdapter();
daB->MissingSchemaAction = MissingSchemaAction::AddWithKey;

SqlCommand ^SelectA = gcnew SqlCommand(
    "SELECT * FROM TableA WHERE <criteria>", connection);
daA->SelectCommand = SelectA;

SqlCommand ^SelectB = gcnew SqlCommand(
    "SELECT * FROM TableB WHERE <criteria + criteria from SelectA>",
connection);
daB->SelectCommand = SelectB;

daA->Fill(ds, "TableA"); //Works Fine - no duplication rows
daB->Fill(ds, "TableB"); //Error

Based on the criteria from the SelectA command, SelectB will find the same
row in TableB more than once.

The error is reported as "Failed to enable constraints: Violates non-null,
unique, or foreign-key".
When I change db->MissingSchemaAction to Add instead of AddWithKey, the
Fill() method succeeds but I have duplicate identical rows in
ds->Tables["TableB"] (not useful).

I thought AddWithKey would update identical rows, but instead is throwing an
exception... Any help with what is happening here or solutions will be
appreciated.

TIA,
M

AddThis Social Bookmark Button