vb.net使用2个where子句更新访问记录

时间:2018-05-11 17:29:40

标签: vb.net ms-access records

我在尝试使用2来自我的数据库的Where值来更新" IddeEmpleado"时出现语法错误。员工ID,其中" Fecha"这意味着日期。这位员工已经开始计时,我希望在时钟输出时更新,我在一小时内得到了时钟。

 Private Sub btnClockout_Click(sender As Object, e As EventArgs) Handles btnClockout.Click
    Dim ds As New DataSet()

    Dim connString As String


    Dim myConnection As OleDbConnection = New OleDbConnection
    connString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = ..\Bases de Datos\Horario.accdb"
    myConnection.ConnectionString = connString

    myConnection.Open()

    Dim cmd As OleDbCommand = New OleDbCommand("select * from Empleado where IdDeEmpleado like '%" + txtEmpId.Text + "%'", myConnection)
    Dim Adpt As New OleDbDataAdapter(cmd)

    If (Adpt.Fill(ds, "Empleado")) Then

        Dim con2 As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = ..\Bases de Datos\Horario.accdb")



        Try
            Dim Finalizoturno As Object
            Finalizoturno = DateTimePicker1.Text

            con2.Open()

            Dim updateQuery As String = "Update EmpleadoHorario Set FinalizoTurno= '" & DateTimePicker1.Text & "' Where Fecha= '" & datePicker.Text & " ' AND IddeEmpleado = '" & txtEmpId.Text & "'"

            Dim cmd2 As New OleDbCommand(updateQuery, con2)

            cmd2.ExecuteNonQuery()





            MessageBox.Show("Salida de Turno Aceptada")

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally
            con2.Dispose()

            End Try
            con2.Close()

        Else

            MessageBox.Show("ID de Empleado no encontrado")




        End If
        myConnection.Close()

End Sub

1 个答案:

答案 0 :(得分:1)

首先,永远,在项目属性,编译选项卡中启用Option Strict。

  1. 您正在创建您不需要的对象。数据集和DataAdapter不仅仅需要计算。

  2. 您可以通过将连接字符串传递给构造函数来在一行中初始化连接。

  3. 在您需要之前不要打开您的连接。

  4. 主键永远不会重复,因此在查询中使用Like子句是没有意义的。您只是检查ID是否存在。当您需要的只是一个计数时,不要检索数据。

  5. 使用参数。 Acces不关心参数的名称,只关心顺序;因此请确保以与查询中显示的顺序相同的顺序添加参数。

  6. 很少需要将变量声明为Object。 Finalizoturno是一个Date,因此将它作为declate。 DateTimePicker的Value属性返回一个日期,Text属性返回一个字符串。

  7. `

    Private Sub btnClockout_Click(sender As Object, e As EventArgs) Handles btnClockout.Click
        Dim myConnection As New OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = ..\Bases de Datos\Horario.accdb")
        Dim updateQuery As String = "Update EmpleadoHorario Set FinalizoTurno= ? Where Fecha= ? AND IddeEmpleado = ?;"
        Dim cmd As New OleDbCommand(updateQuery, myConnection)
        Try
            cmd.Parameters.Add("Fecha1", OleDbType.Date).Value = DateTimePicker1.Value
            cmd.Parameters.Add("Fecha2", OleDbType.Date).Value = datePicker.Value
            cmd.Parameters.Add("ID", OleDbType.Integer).Value = CInt(txtEmpId.Text)
            myConnection.Open()
            Dim retVal As Integer = cmd.ExecuteNonQuery()
            myConnection.Close()
            If retVal = 1 Then
                MessageBox.Show("Salida de Turno Aceptada")
            Else               
                MessageBox.Show("ID de Empleado no encontrado")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            cmd.Dispose()
            myConnection.Dispose()
        End Try
    End Sub
    

    `