VB中的UPDATE语句连接到Access

时间:2016-04-19 05:05:38

标签: vb.net notifications

我正在尝试更新访问数据库中的位置列,但问题是我有问题更新该列,而列的其余部分不会发出错误消息。

  

错误消息:UPDATE语句,Microsoft JET数据库引擎中的语法错误...

代码:

Dim myConnection As OleDbConnection = New OleDbConnection

Dim ds As New DataSet
Dim da As OleDbDataAdapter
Dim MaxRows As Integer
Dim i As Integer
Dim sql As String


Private Sub updateButton_Click(sender As Object, e As EventArgs) Handles updateButton.Click
    Using myConnection = New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =C:\LecturerDetail.mdb")

        myConnection.Open()

        Dim str As String

        str = "UPDATE lecturer " & _
              "SET [empName] = ?,[empId] = ?, [position] =?, [faculty] = ? " & _
              " , [degree1] = ?, [degree2] = ?, [degree3] = ?,[degree] = ?, [empType] = ? " & _
              " ,[icNo] = ?, [citizenship] = ?, [phoneNo] = ?, [email] = ?,[permitNo] = ? " & _
              " , [permitStartDate] = ?, [permitEndDate] = ?, [pStatus] =?, [remark] =? " & _
              " WHERE ([empId] = ?) "

        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        cmd.Parameters.AddWithValue("@empName", nameTxt.Text)
        cmd.Parameters.AddWithValue("@empId", empIdTxt.Text)
        cmd.Parameters.AddWithValue("@position", positionComboBox.SelectedText)
        cmd.Parameters.AddWithValue("@faculty", facultyComboBox.SelectedText)
        cmd.Parameters.AddWithValue("@degree1", empDeg1.Text)
        cmd.Parameters.AddWithValue("@degree2", empDeg2.Text)
        cmd.Parameters.AddWithValue("@degree3", empDeg3.Text)
        cmd.Parameters.AddWithValue("@degree", empDeg4.Text)
        cmd.Parameters.AddWithValue("@empType", empTypeComboBox.SelectedText)
        cmd.Parameters.AddWithValue("@icNo", icTxt.Text)
        cmd.Parameters.AddWithValue("@citizenship", citizenshipComboBox.SelectedText)
        cmd.Parameters.AddWithValue("@phoneNo", phoneTxt.Text)
        cmd.Parameters.AddWithValue("@email", emailTxt.Text)
        cmd.Parameters.AddWithValue("@permitNo", permitNoTxt.Text)
        cmd.Parameters.AddWithValue("@permitStartDate", DateTimePicker1.Text)
        cmd.Parameters.AddWithValue("@permitEndDate", DateTimePicker2.Text)
        cmd.Parameters.AddWithValue("@pStatus", statusComboBox.Text)
        cmd.Parameters.AddWithValue("@remark", remark.Text)
        cmd.Parameters.AddWithValue("@empId", empIdTxt.Text)

        Try
            cmd.ExecuteNonQuery()
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            ds.Tables("lecturer").Rows(i).Item(0) = empIdTxt.Text
            ds.Tables("lecturer").Rows(i).Item(1) = nameTxt.Text
            ds.Tables("lecturer").Rows(i).Item(2) = positionComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(3) = facultyComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(4) = empDeg1.Text
            ds.Tables("lecturer").Rows(i).Item(5) = empDeg2.Text
            ds.Tables("lecturer").Rows(i).Item(6) = empDeg3.Text
            ds.Tables("lecturer").Rows(i).Item(7) = empDeg4.Text
            ds.Tables("lecturer").Rows(i).Item(8) = empTypeComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(9) = icTxt.Text
            ds.Tables("lecturer").Rows(i).Item(10) = citizenshipComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(11) = phoneTxt.Text
            ds.Tables("lecturer").Rows(i).Item(12) = emailTxt.Text
            ds.Tables("lecturer").Rows(i).Item(13) = permitNoTxt.Text
            ds.Tables("lecturer").Rows(i).Item(14) = DateTimePicker1.Value
            ds.Tables("lecturer").Rows(i).Item(15) = DateTimePicker2.Value
            ds.Tables("lecturer").Rows(i).Item(16) = statusComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(17) = remark.Text

            da.Update(ds, "lecturer")
            ds.AcceptChanges()
            myConnection.Close()
            MsgBox("Record Updated")
        Catch ex As Exception
            MessageBox.Show(ex.Message & "-" & ex.Source)
        End Try
    End Using
End Sub

我的代码是更新列并允许用户导航到下一条记录。

我是视觉基础的新手,所以我们非常感谢您的详细描述和指导。谢谢。另外,我试图根据日期创建自动通知系统。任何人都可以告诉我在visual basic中使用哪些方法或应用程序来实现它。

1 个答案:

答案 0 :(得分:0)

无需创建更新查询手。 CommandBuilder本身生成更新数据库的必要指令。

Dim ds As New DataSet
Dim da As OleDbDataAdapter
Dim MaxRows As Integer
Dim i As Integer
Dim sql As String

Private Sub updateButton_Click(sender As Object, e As EventArgs) Handles updateButton.Click
 Using myConnection = New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =C:\LecturerDetail.mdb")
        myConnection.Open()        
        Dim str As String
        str = "SELECT * FROM lecturer WHERE ([empId] = @empId)"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)       
        cmd.Parameters.AddWithValue("@empId", empIdTxt.Text)

        Try
            da = new OleDbDataAdapter(cmd)
            da.Fill(ds,"lecturer")            
            ds.Tables("lecturer").Rows(i).Item(0) = empIdTxt.Text
            ds.Tables("lecturer").Rows(i).Item(1) = nameTxt.Text
            ds.Tables("lecturer").Rows(i).Item(2) = positionComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(3) = facultyComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(4) = empDeg1.Text
            ds.Tables("lecturer").Rows(i).Item(5) = empDeg2.Text
            ds.Tables("lecturer").Rows(i).Item(6) = empDeg3.Text
            ds.Tables("lecturer").Rows(i).Item(7) = empDeg4.Text
            ds.Tables("lecturer").Rows(i).Item(8) = empTypeComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(9) = icTxt.Text
            ds.Tables("lecturer").Rows(i).Item(10) = citizenshipComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(11) = phoneTxt.Text
            ds.Tables("lecturer").Rows(i).Item(12) = emailTxt.Text
            ds.Tables("lecturer").Rows(i).Item(13) = permitNoTxt.Text
            ds.Tables("lecturer").Rows(i).Item(14) = DateTimePicker1.Value
            ds.Tables("lecturer").Rows(i).Item(15) = DateTimePicker2.Value
            ds.Tables("lecturer").Rows(i).Item(16) = statusComboBox.Text
            ds.Tables("lecturer").Rows(i).Item(17) = remark.Text
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            da.Update(ds, "lecturer")
            ds.AcceptChanges()
            myConnection.Close()
            MsgBox("Record Updated")
        Catch ex As Exception
            MessageBox.Show(ex.Message & "-" & ex.Source)
        End Try

    End Using
End Sub