M
Marina
Hi, consider the following scenario: A new row gets inserted into a
datatable. Some of the fields are filled in, others are left as NULL. Then
Update gets called using the SqlCommandBuilder to build the commands. The
database table this insert is going into has default values defined for
fields. So the fields that were not explicitly set, get actual values. This
same row is later edited and Update is called again - this time to update
the row. This of course fails, since the database row has the default
values for some fields, but the code's DataRow still has NULL's.
So it is clear why the problem occurs. What isn't clear, is a way to avoid
it.
The SqlCommandBuilder creates a WHERE clause that includes the original
values for every single column - not just the primary key. Shouldn't there
be an option to have the WHERE clause be generated based on primary key
columns only? This would be a really easy way to solve the problem -
unfortunately it isn't there.
Ok, so decided to try using MissingShemaAction.AddWithKey to get all the
primary key information, to maybe re-do the WHERE clause manually. BUT,
this downloads the fact that some columns are NOT NULL in the database.
However, it does not download what their default value is (even though
DataColumn has a DefaultValue property - it isn't being populated). So now,
you get an error even before a trip to the database is made, since the .NET
classes are checking the integrity of this field. Now why would one piece
of information be retrieved and not the other? (This is documented as such,
but doesn't make sense).
So really, the solution seems to have to manually define
update/insert/delete commands for each table. We are trying to create a
generic component that is capable of performing update of what could be one
of hundreds of tables, used in a variety of ways. Coding the update to work
with hundreds of tables, and keeping track of which table has what primary
key columns, and then manually creating all the statements seems a bit much?
Any thoughts or other suggestions?
datatable. Some of the fields are filled in, others are left as NULL. Then
Update gets called using the SqlCommandBuilder to build the commands. The
database table this insert is going into has default values defined for
fields. So the fields that were not explicitly set, get actual values. This
same row is later edited and Update is called again - this time to update
the row. This of course fails, since the database row has the default
values for some fields, but the code's DataRow still has NULL's.
So it is clear why the problem occurs. What isn't clear, is a way to avoid
it.
The SqlCommandBuilder creates a WHERE clause that includes the original
values for every single column - not just the primary key. Shouldn't there
be an option to have the WHERE clause be generated based on primary key
columns only? This would be a really easy way to solve the problem -
unfortunately it isn't there.
Ok, so decided to try using MissingShemaAction.AddWithKey to get all the
primary key information, to maybe re-do the WHERE clause manually. BUT,
this downloads the fact that some columns are NOT NULL in the database.
However, it does not download what their default value is (even though
DataColumn has a DefaultValue property - it isn't being populated). So now,
you get an error even before a trip to the database is made, since the .NET
classes are checking the integrity of this field. Now why would one piece
of information be retrieved and not the other? (This is documented as such,
but doesn't make sense).
So really, the solution seems to have to manually define
update/insert/delete commands for each table. We are trying to create a
generic component that is capable of performing update of what could be one
of hundreds of tables, used in a variety of ways. Coding the update to work
with hundreds of tables, and keeping track of which table has what primary
key columns, and then manually creating all the statements seems a bit much?
Any thoughts or other suggestions?