I am fairly new to .net development but I have worked with Access and SQL for over 14 years. Hence to say, I should know at least a few things about database development. I just ran into a situation where I had to remove a constraint within the SQL database because VB.net could not process data in the correct order.
It was a "simple" master/detail setup, for practical purposes, I will use the order/order detail example. First you enter the order header and then enter the order lines. In SQL there is a constraint on the Orders table and OrderLines table based on the OrderID field with cascade delete enforced. The OrderID is a hidden "meaningless" key so it will never change.
When I save data in my form to the SQL database, I save the header first, then the lines. I must do this because the constraint will NOT allow any OrderLine records without a matching Order record. This is good DB design!
When creating a DataSet with the Data Wizard or by hand coding, the code used to trigger the save to the DB is.
Although the .update command is used, this is misleading, it will not just trigger an update, it will trigger an INSERT, UPDATE or DELETE statement based on the action required relevant to the row that it is currently processing.
This is where the problem comes in. Like I said earlier, I must save the Order HEADER first, then the Order LINES. However, when an order is DELETED, you need to save the Order LINES first, then Order Header to prevent a conflict with the SQL constraint.
So to solve the issue, I had to remove the Database constraint which I would argue is bad database design! Unless I was to cycle through all of the datatables and write my own individual INSERT, UPDATE & DELETE logic, I cannot see a way around this.
Anyone have any suggestions?
It was a "simple" master/detail setup, for practical purposes, I will use the order/order detail example. First you enter the order header and then enter the order lines. In SQL there is a constraint on the Orders table and OrderLines table based on the OrderID field with cascade delete enforced. The OrderID is a hidden "meaningless" key so it will never change.
When I save data in my form to the SQL database, I save the header first, then the lines. I must do this because the constraint will NOT allow any OrderLine records without a matching Order record. This is good DB design!
When creating a DataSet with the Data Wizard or by hand coding, the code used to trigger the save to the DB is.
VB.NET:
OrderAdapter.Update(OrderDataSet, "Orders")
OrderLinesAdapter.Update(OrderDataSet, "OrderLines")
Although the .update command is used, this is misleading, it will not just trigger an update, it will trigger an INSERT, UPDATE or DELETE statement based on the action required relevant to the row that it is currently processing.
This is where the problem comes in. Like I said earlier, I must save the Order HEADER first, then the Order LINES. However, when an order is DELETED, you need to save the Order LINES first, then Order Header to prevent a conflict with the SQL constraint.
So to solve the issue, I had to remove the Database constraint which I would argue is bad database design! Unless I was to cycle through all of the datatables and write my own individual INSERT, UPDATE & DELETE logic, I cannot see a way around this.
Anyone have any suggestions?
Last edited: