保存数据库问题

时间:2018-01-30 21:04:06

标签: vb.net visual-studio

基本上,我相信我使用的是正确的代码,但数据库仍然无法更新。它适用于当前会话,但是,当我停止并重新启动程序时,似乎数据尚未在数据库中更新。

真正有趣的部分是我使用相同的方法在其他地方更新数据库,在使用和重新启动会话时,数据库已经更新。

P.S。我也在两个表单上都设置了相同的适配器和绑定源等 我很困惑,请帮助

我相信的代码是正确但不起作用的代码:(在另一个表单上更新,所以我有一个地方所有表单都更新因此FRMMain等。)

Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
    Dim CurrentPoints As Integer
    Dim UpdatedPoints As Integer

    CurrentPoints = FRMMain.MyDBDataSet.Tables("TBLPupil").Rows(looopcount)(15)
    UpdatedPoints = CurrentPoints + stfPoints
    FRMMain.MyDBDataSet.Tables("TBLPupil").Rows(looopcount)(15) = UpdatedPoints

    FRMMain.TBLPupilTableAdapter.Update(MyDBDataSet.TBLPupil)
    FRMMain.TBLPupilTableAdapter.Fill(MyDBDataSet.TBLPupil)

End Sub

我正在使用的另一种形式的代码:

    Private Sub BtnYes_Click(sender As Object, e As EventArgs) Handles BtnYes.Click
    Dim Points As Integer = FRMPupil.Pointss
    Dim Cost As Integer = FRMPupil.RewardCost
    Points = Points - Cost
    FRMPupil.LePoints = Points

    MyDBDataSet.Tables("TBLPupil").Rows(FRMLogin.DBLocation)(15) = Points
    FRMMain.TBLPupilTableAdapter.Update(MyDBDataSet.TBLPupil)
    FRMMain.TBLPupilTableAdapter.Fill(MyDBDataSet.TBLPupil)

    Me.Hide()

End Sub

1 个答案:

答案 0 :(得分:0)

  

我的代码是正确的,但无效。

,如果不能正常工作,那就不正确了!

你可以做不同的事情:干,Dont Repeat Yourself。您正在重复代码,以便在代码中的多个位置更新点。这很容易出错。写一次并重复使用,例如通过应用Repository Pattern。它可以更容易地检测错误并纠正错误。它允许您重用已在其他方案中测试过的代码(在另一种形式上)。

调试,调试,调试。将断点放在不工作的方法中,看看会发生什么。所有变量都具有预期值吗?例如,looopcountFRMLogin.DBLocation具有相同的价值吗?某处肯定存在差异。请参阅:Navigating through Code with the Debugger或更新的文章Debug your Hello World application with Visual Studio 2017

相关问题