VB.Net更新数据库不工作

时间:2015-10-30 10:14:08

标签: vb.net ms-access

我尝试将数据添加到数据库并在更改现有条目时更新数据。 INSERT命令工作正常,但UPDATE命令正确执行但无法在数据库中进行任何更改。

我已删除了标签名称并使用了ABC等等,因为我无法识别我正在工作的项目在,但它没有任何区别,因为它只是标签名称。

下面是代码:

Module Database_Manipulation
    #Region "Adding To The Database"

        Public Sub Send_To_Database()

            Dim myCon = New OleDbConnection(My.Settings.AMD_ConnectionString)
            Dim sqrtext As String
            myCon.Open()

            If Common_Globals.New_Record = True Then

                sqrtext = "INSERT INTO [AMD_DATA] ([A], [B], [C], [D], [E], [F], [G], [H], [I], " & _
                    "[J], [K], [L], [M], [N], [O], [P], " & _
                    "[Q], [R], [S], [T], [U], " & _
                    "[V], [W], [X], [Y], [Z], " & _
                    "[AA], [AB], [AC], [AD], [AE], " & _
                    "[AF], [AG], [AH], [AI], [AJ])" & _
                    "VALUES (@DOR, @AOR, @AOS, @NOC, @CDOB, @EOC, @CRAD, @CIC, @ANOC, @NOS, @SDOB, @ESOS, @ETS, @LOSWC, @EOS, @ANOS, " & _
                    "@IDT, @PAC, @SAC, @RR, @AAN, @IEMD, @IEMAT, @IEMAC, @IEMOU, @IEMCO, @PAMD, @PAMAT, @PAMAC, @PAMOU, @PAMCO, " & _
                    "@AO, @AO2, @AO3, @ACC, @DCC)"


                Dim sqr As OleDbCommand = New OleDbCommand(sqrtext, myCon)

                sqr.Parameters.Add("@DOR", OleDbType.DBDate).Value = Form1.A.Value

                sqr.Parameters.Add("@AOR", OleDbType.VarChar).Value = Form1.B.Text

                sqr.Parameters.Add("@AOS", OleDbType.VarChar).Value = Form1.C.Text

                sqr.Parameters.Add("@NOC", OleDbType.VarChar).Value = Form1.D.Text

                sqr.Parameters.Add("@CDOB", OleDbType.DBDate).Value = Form1.E.Value

                sqr.Parameters.Add("@EOC", OleDbType.VarChar).Value = Form1.F.Text

                If Form1.G.Checked = True Then
                    sqr.Parameters.Add("@CRAD", OleDbType.Boolean).Value = True
                Else
                    sqr.Parameters.Add("@CRAD", OleDbType.Boolean).Value = False
                End If

                If Form1.H.Checked = True Then
                    sqr.Parameters.Add("@CIC", OleDbType.Boolean).Value = True
                Else
                    sqr.Parameters.Add("@CIC", OleDbType.Boolean).Value = False
                End If

                sqr.Parameters.Add("@ANOC", OleDbType.VarChar).Value = Form1.I.Text

                sqr.Parameters.Add("@NOS", OleDbType.VarChar).Value = Form1.J.Text

                sqr.Parameters.Add("@SDOB", OleDbType.DBDate).Value = Form1.K.Value

                sqr.Parameters.Add("@ESOS", OleDbType.VarChar).Value = Form1.L.Text

                sqr.Parameters.Add("@ETS", OleDbType.VarChar).Value = Form1.M.Text

                sqr.Parameters.Add("@LOSWC", OleDbType.VarChar).Value = Form1.N.Text

                sqr.Parameters.Add("@EOS", OleDbType.VarChar).Value = Form1.O.Text

                sqr.Parameters.Add("@ANOS", OleDbType.VarChar).Value = Form1.P.Text

                sqr.Parameters.Add("@IDT", OleDbType.VarChar).Value = Form1.Q.Text

                sqr.Parameters.Add("@PAC", OleDbType.VarChar).Value = Form1.R.Text

                sqr.Parameters.Add("@SAC", OleDbType.VarChar).Value = Form1.S.Text

                If Form1.T.Checked = True Then
                    sqr.Parameters.Add("@RR", OleDbType.Boolean).Value = True
                Else
                sqr.Parameters.Add("@RR", OleDbType.Boolean).Value = False
                End If

                sqr.Parameters.Add("@AAN", OleDbType.VarChar).Value = Form1.U.Text

                sqr.Parameters.Add("@IEMD", OleDbType.DBDate).Value = Form1.V.Value

                sqr.Parameters.Add("@IEMAT", OleDbType.VarChar).Value = Form1.W.Text

                sqr.Parameters.Add("@IEMAC", OleDbType.VarChar).Value = Form1.X.Text

                sqr.Parameters.Add("@IEMOU", OleDbType.VarChar).Value = Form1.Y.Text

                sqr.Parameters.Add("@IEMCO", OleDbType.VarChar).Value = Form1.Z.Text

                sqr.Parameters.Add("@PAMD", OleDbType.DBDate).Value = Form1.AA.Value

                sqr.Parameters.Add("@PAMAT", OleDbType.VarChar).Value = Form1.AB.Text

                sqr.Parameters.Add("@PAMAC", OleDbType.VarChar).Value = Form1.AC.Text

                sqr.Parameters.Add("@PAMOU", OleDbType.VarChar).Value = Form1.AD.Text

                sqr.Parameters.Add("@PAMCO", OleDbType.VarChar).Value = Form1.AE.Text

                sqr.Parameters.Add("@AO", OleDbType.VarChar).Value = Form1.AF.Text

                sqr.Parameters.Add("@AO2", OleDbType.VarChar).Value = Form1.AG.Text

                sqr.Parameters.Add("@AO3", OleDbType.VarChar).Value = Form1.AH.Text

                sqr.Parameters.Add("@ACC", OleDbType.VarChar).Value = Form1.AI.Text

                sqr.Parameters.Add("@DCC", OleDbType.DBDate).Value = Form1.AJ.Value



                Try
                    Dim command = sqr.ExecuteNonQuery()

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

            Else

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'THIS IS THE SECTION THAT ISN'T WORKING CORRETLY
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


                sqrtext = "UPDATE [AMD_DATA] Set [A] = @DOR, " & _
                    "[B] = @AOR, " & _
                    "[C] = @AOS, " & _
                    "[D] = @NOC, " & _
                    "[E] = @CDOB, " & _
                    "[F] = @EOC, " & _
                    "[G] = @CRAD, " & _
                    "[H] = @CIC, " & _
                    "[I] = @ANOC, " & _
                    “[J] = @NOS, " & _
                    "[K] = @SDOB, " & _
                    "[L] = @ESOS, " & _
                    "[M] = @ETS, " & _
                    "[N] = @LOSWC, " & _
                    "[O] = @EOS, " & _
                    "[P] = @ANOS, " & _
                    "[Q] = @IDT, " & _
                    "[R] = @PAC, " & _
                    "[S] = @SAC, " & _
                    "[T] = @RR, " & _
                    "[U] = @AAN, " & _
                    "[V] = IEMD, " & _
                    "[W] = @IEMAT,  " & _
                    "[X] = @IEMAC, " & _
                    "[Y] = @IEMOU, " & _
                    "[Z] = @IEMCO, " & _
                    "[AA] = @PAMD,  " & _
                    "[AB] = @PAMAT, " & _
                    "[AC] = @PAMAC,  " & _
                    "[AD] = @PAMOU, " & _
                    "[AE] = @PAMCO, " & _
                    "[AF] = @AO, " & _
                    "[AG] = @AO2, " & _
                    "[AH] = @AO3," & _
                    "[AI] = @ACC,  " & _
                    "[AJ] = @DCC " & _
                    "WHERE [Reference ID] = @REF"

                Dim sqr As OleDbCommand = New OleDbCommand(sqrtext, myCon)

                sqr.Parameters.AddWithValue("@DOR", Form1.A.Value)

                sqr.Parameters.AddWithValue("@AOR", Form1.B.Text)

                sqr.Parameters.AddWithValue("@AOS", Form1.C.Text)

                sqr.Parameters.AddWithValue("@NOC", Form1.D.Text)

                sqr.Parameters.AddWithValue("@CDOB", Form1.E.Value)

                sqr.Parameters.AddWithValue("@EOC", Form1.F.Text)

                If Form1.G.Checked = True Then
                    sqr.Parameters.AddWithValue("@CRAD", True)
                Else
                    sqr.Parameters.AddWithValue("@CRAD", False)
                End If

                If Form1.H.Checked = True Then
                    sqr.Parameters.AddWithValue("@CIC", True)
                Else
                    sqr.Parameters.AddWithValue("@CIC", False)
                End If

                sqr.Parameters.AddWithValue("@ANOC", Form1.I.Text)

                sqr.Parameters.AddWithValue("@NOS", Form1.J.Text)

                sqr.Parameters.AddWithValue("@SDOB", Form1.K.Value)

                sqr.Parameters.AddWithValue("@ESOS", Form1.L.Text)

                sqr.Parameters.AddWithValue("@ETS", Form1.M.Text)

                sqr.Parameters.AddWithValue("@LOSWC", Form1.N.Text)

                sqr.Parameters.AddWithValue("@EOS", Form1.O.Text)

                sqr.Parameters.AddWithValue("@ANOS", Form1.P.Text)

                sqr.Parameters.AddWithValue("@IDT", Form1.Q.Text)

                sqr.Parameters.AddWithValue("@PAC", Form1.R.Text)

                sqr.Parameters.AddWithValue("@SAC", Form1.S.Text)

                If Form1.T.Checked = True Then
                    sqr.Parameters.AddWithValue("@RR", True)
                Else
                    sqr.Parameters.AddWithValue("@RR", False)
                End If

                sqr.Parameters.AddWithValue("@AAN", Form1.U.Text)

                sqr.Parameters.AddWithValue("@IEMD", Form1.V.Value)

                sqr.Parameters.AddWithValue("@IEMAT", Form1.W.Text)

                sqr.Parameters.AddWithValue("@IEMAC", Form1.X.Text)

                sqr.Parameters.AddWithValue("@IEMOU", Form1.Y.Text)

                sqr.Parameters.AddWithValue("@IEMCO", Form1.Z.Text)

                sqr.Parameters.AddWithValue("@PAMD", Form1.AA.Value)

                sqr.Parameters.AddWithValue("@PAMAT", Form1.AB.Text)

                sqr.Parameters.AddWithValue("@PAMAC", Form1.AC.Text)

                sqr.Parameters.AddWithValue("@PAMOU", Form1.AD.Text)

                sqr.Parameters.AddWithValue("@PAMCO", Form1.AE.Text)

                sqr.Parameters.AddWithValue("@AO", Form1.AF.Text)

                sqr.Parameters.AddWithValue("@AO2", Form1.AG.Text)

                sqr.Parameters.AddWithValue("@AO3", Form1.AH.Text)

                sqr.Parameters.AddWithValue("@ACC", Form1.AI.Text)

                sqr.Parameters.AddWithValue("@DCC", Form1.AJ.Value)

                sqr.Parameters.AddWithValue("@REF", Form1.Reference_IDTextBox.Text)

                Try
                    Dim command2 = sqr.ExecuteNonQuery()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If

            Form1.TableAdapter.Fill(Form1.AMD_Dataset.AMD_DATA)


            FAM.setupdatagrids() ‘Simply fills the datagrid with new values

            myCon.Close()
        End Sub

    #End Region


    End Module

也许我错过了什么?

1 个答案:

答案 0 :(得分:0)

以sqrtext为单位,IEMD的条目缺少@

                "[U] = @AAN, " & _
                "[V] = IEMD, " & _
                "[W] = @IEMAT,  " & _