保存时UPDATE语句出错

时间:2016-05-22 04:12:39

标签: vb.net ms-access-2010 vb.net-2010

我有包含数据的文本框。然后,如果我单击更新它将启用所有文本框,以便我可以编辑它们。如果我再次单击“更新”,它将保存但是 我不断收到错误消息

" UPDATE语句中的语法错误"

        myConnection.Open()
    Dim str As String = "UPDATE StudentDatabase set FName='" & FNameTextBox.Text & "',MName='" & MNameTextBox.Text & "',LName='" & LNameTextBox.Text & "' ,DOB='" & DOBDateTimePicker.Text & "',Gender='" & GenderTextBox.Text & "',Address='" & AddressTextBox.Text & "',Section='" & SectionTextBox.Text & "',FatherName='" & FatherNameTextBox.Text & "',FatherOccupation='" & FatherOccupationTextBox.Text & "',FatherContact='" & FatherContactTextBox.Text & "',MotherName='" & MotherNameTextBox.Text & "',MotherOccupation='" & MotherOccupationTextBox.Text & "',MotherContact='" & MotherContactTextBox.Text & "',Guardian='" & GuardianTextBox.Text & "',GuardianContact='" & GuardianContactTextBox.Text & "' where StudID='" & StudIDTextBox.Text & "'   "
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    If (LNameTextBox.ReadOnly = True) Then
        FNameTextBox.ReadOnly = False
        LNameTextBox.ReadOnly = False
        MNameTextBox.ReadOnly = False
        GenderTextBox.ReadOnly = False
        AddressTextBox.ReadOnly = False
        SectionTextBox.ReadOnly = False
        FatherNameTextBox.ReadOnly = False
        FatherContactTextBox.ReadOnly = False
        FatherOccupationTextBox.ReadOnly = False
        MotherNameTextBox.ReadOnly = False
        MotherContactTextBox.ReadOnly = False
        MotherOccupationTextBox.ReadOnly = False
        GuardianContactTextBox.ReadOnly = False
        GuardianTextBox.ReadOnly = False
    ElseIf (LNameTextBox.ReadOnly = False) Then

        Try


            cmd.ExecuteNonQuery()


            FNameTextBox.ReadOnly = True
            LNameTextBox.ReadOnly = True
            MNameTextBox.ReadOnly = True
            GenderTextBox.ReadOnly = True
            AddressTextBox.ReadOnly = True
            SectionTextBox.ReadOnly = True
            FatherNameTextBox.ReadOnly = True
            FatherContactTextBox.ReadOnly = True
            FatherOccupationTextBox.ReadOnly = True
            MotherNameTextBox.ReadOnly = True
            MotherContactTextBox.ReadOnly = True
            MotherOccupationTextBox.ReadOnly = True
            GuardianContactTextBox.ReadOnly = True
            GuardianTextBox.ReadOnly = True
            myConnection.Close()

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

    End If

1 个答案:

答案 0 :(得分:0)

首先,在您被警告时,请使用参数化查询。如果异常没有为您提供足够的信息,请考虑将完整的查询写入控制台,以便您可以方便地检查语法。 dobdatetimepicker.text返回的是什么格式,这是您的数据库所期望的(但是,如果我没有记错,您会收到不同的错误消息)?如果这些都没有显示问题,请从查询字符串中删除输入,直到发生不同的事情。

相关问题