问题
I have a data table (Named "Effects") from Access in my VB.Net Userform.
I have two buttons (AddEffect.Click and SaveEffect.Click) that use ADD NEW, END EDIT and UPDATEALL. This works whilst the app is open.
A new record appears in the GRIDVIEW with the Data I inputed into the various textboxes.
Private Sub AddEffect_Click(sender As Object, e As EventArgs) Handles AddEffect.Click
Me.EffectsBindingSource.AddNew()
End Sub
Private Sub SaveEffect_Click(sender As Object, e As EventArgs) Handles SaveEffect.Click
Me.EffectsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DatabaseDataSet)
End Sub
However, there are two issues.
- The ID Number of the New Record generated is always a -ve integer.
- When I click the SAVE ENTRY button and restart the application, the changes have not been saved.
Other examples I look at online do not resemble my issue.
EDIT:
回答1:
The ID Number of the New Record generated is always a -ve integer.
That's exactly as it should be. The value generated by your application is only temporary. The final ID is generated by the database when you save. The temp value is negative to ensure that it doesn't clash with anything that's already in the database.
When I click the SAVE ENTRY button and restart the application, the changes have not been saved.
You are almost certainly over-writing your database. The way file-based databases should work is that you add a source file to your project and that gets copied to the output folder along with your EXE. It's that copy that your app connects to while you're debugging. If you create a new copy the next time you build, which is the default, you lose any changes you made to the old copy.
To avoid that, select your data file in the Solution Explorer, open the Properties window and set Copy to output directory to Copy if newer. That way, a new copy will only be created if you explicitly delete the old copy or make a change to the source file.
The reason for the dual-file approach is that you're supposed to keep your source file clean and free of test data so that the copy created when you build a Release version of your app will be ready to deploy with a fresh app.
来源:https://stackoverflow.com/questions/64334393/what-am-i-missing-in-my-approach-to-add-new-records-to-my-database-in-vb-net-app