B
Brad Williams
This KB article describes a fix for a problem ("by design"!) using
SqlDataAdapter.Update followed by a DataSet.Merge:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q313540
The problem addressed is: The default behavior of SqlDataAdapter.Update sets
the private state of certain rows of the dataset incorrectly, so that if
that dataset was originally derived from a source dataset using
DataSet.GetChanges, then the updated "changes" dataset cannot be correctly
merged back into the source dataset (you get duplicate rows). This happens
when the primary key of the dataset is an identity column generated by the
DB.
The fix in the KB article boils down to adding an event handler to the
SqlDataAdapter like this:
private void dataAdapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
if (e.StatementType == StatementType.Insert)
e.Status = UpdateStatus.SkipCurrentRow;
}
My question is: Are there any other side-effects of using this fix, or
situations where it will screw things up? Is there any reason not to ALWAYS
use this event handler?
A secondary question would be: Why does the article say to "check and
update each row manually instead of using AcceptChanges", but in their
example fix they don't follow this advice, and it seems to work out just
fine for them?
Thanks,
Brad Williams
SqlDataAdapter.Update followed by a DataSet.Merge:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q313540
The problem addressed is: The default behavior of SqlDataAdapter.Update sets
the private state of certain rows of the dataset incorrectly, so that if
that dataset was originally derived from a source dataset using
DataSet.GetChanges, then the updated "changes" dataset cannot be correctly
merged back into the source dataset (you get duplicate rows). This happens
when the primary key of the dataset is an identity column generated by the
DB.
The fix in the KB article boils down to adding an event handler to the
SqlDataAdapter like this:
private void dataAdapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
if (e.StatementType == StatementType.Insert)
e.Status = UpdateStatus.SkipCurrentRow;
}
My question is: Are there any other side-effects of using this fix, or
situations where it will screw things up? Is there any reason not to ALWAYS
use this event handler?
A secondary question would be: Why does the article say to "check and
update each row manually instead of using AcceptChanges", but in their
example fix they don't follow this advice, and it seems to work out just
fine for them?
Thanks,
Brad Williams