检查列是否为空但我收到错误。(Vb.net)

Checking if Column is empty but i am receiving Error.(Vb.net)

我想在插入日期之前检查一列是否为空,但我收到了一条错误消息,我不知道如何修复它。我读到了 ExecuteScalar(),但对我来说没有帮助。

代码:

Dim sql As String = "UPDATE [TA-Arbeitszeit] SET Ende = @ende WHERE Personal_nr = @Personal_nr AND Arbeitstag = @Arbeitstag"
Dim sql2 As String = "SELECT * FROM [TA-Arbeitszeit] WHERE Personal_nr = @Personal_Nr AND Arbeitstag = @Arbeitstag"

Using conn2 As New OleDbConnection(connectionString),
    cmd2 As New OleDbCommand(sql2, conn2)
    conn2.Open()
    cmd2.Parameters.Add("@Personal_nr", OleDbType.VarChar).Value = tbxUserInput.Text.Trim()
    cmd2.Parameters.Add("@Arbeitstag", OleDbType.VarChar).Value = DateTime.Now.ToString("d")

    Dim ende As String

    Using reader0 As OleDbDataReader = cmd2.ExecuteReader()
        reader0.Read()
        ende = reader0(3)
    End Using

    If String.IsNullOrEmpty(ende) Then
        Using conn As New OleDbConnection(connectionString),
                cmd As New OleDbCommand(sql, conn)
            conn.Open()
            cmd.Parameters.Add("@ende", OleDbType.VarChar).Value = DateTime.Now.ToString("G")
            cmd.Parameters.Add("@Personal_nr", OleDbType.VarChar).Value = tbxUserInput.Text.Trim()
            cmd.Parameters.Add("@Arbeitstag", OleDbType.VarChar).Value = DateTime.Now.ToString("d")
            Dim icount As Integer = cmd.ExecuteNonQuery

        End Using

        frmGreetings.Label1.Text = eveninggreetingsAray()
        frmGreetings.Label2.Text = "Sie haben Ihren Arbeitstag beendet."
        frmGreetings.ShowDialog()
        tbxUserInput.Select()
    Else
        frmNotificationBox.Label1.Text = "Ihren Arbeitstag ist schon beendet!"
        frmNotificationBox.ShowDialog()
        tbxUserInput.Select()
    End If
End Using

我认为 sql2 中存在逻辑错误。现有记录的日期怎么可能是现在?也许这是在插入后立即被检查的。在这种情况下,为什么不将 end 设为必填字段?

不要从服务器中提取额外的数据。您只关心此子中的 Ende,因此这是要检索的唯一字段。然后你可以使用.ExecuteScalar。我认为 .ToString 可能会解决 null 问题。

将 Connection.Open 直接移动到 .Execute

之前
Private connectionString As String = "Your connection string"
Private Sub OpCode()
    'How can Arbeitstag be Now in an existing record?
    'Dim sql2 As String = "SELECT * FROM [TA-Arbeitszeit] WHERE Personal_nr = @Personal_Nr AND Arbeitstag = @Arbeitstag"
    Dim sql2 As String = "Select Ende From [TA-Arbeitszeit] WHERE Personal_nr = @Personal_Nr AND Arbeitstag = @Arbeitstag;"
    Dim ende As String
    Using conn2 As New OleDbConnection(connectionString),
        cmd2 As New OleDbCommand(sql2, conn2)

        cmd2.Parameters.Add("@Personal_nr", OleDbType.VarChar).Value = tbxUserInput.Text.Trim()
        'I don't quite see how an existing record can have today's date
        cmd2.Parameters.Add("@Arbeitstag", OleDbType.VarChar).Value = DateTime.Now.ToString("d")

        conn2.Open() 'Move the .Open to directly befor the .Execute
        ende = cmd2.ExecuteScalar.ToString
    End Using

    If String.IsNullOrEmpty(ende) Then
        'Move this closer to where is is used
        Dim sql As String = "UPDATE [TA-Arbeitszeit] SET Ende = @ende WHERE Personal_nr = @Personal_nr AND Arbeitstag = @Arbeitstag"

        Using conn As New OleDbConnection(connectionString),
                cmd As New OleDbCommand(sql, conn)
            conn.Open()
            cmd.Parameters.Add("@ende", OleDbType.VarChar).Value = DateTime.Now.ToString("G")
            cmd.Parameters.Add("@Personal_nr", OleDbType.VarChar).Value = tbxUserInput.Text.Trim()
            cmd.Parameters.Add("@Arbeitstag", OleDbType.VarChar).Value = DateTime.Now.ToString("d")
            Dim icount As Integer = cmd.ExecuteNonQuery
        End Using

        frmGreetings.Label1.Text = eveninggreetingsAray()
        frmGreetings.Label2.Text = "Sie haben Ihren Arbeitstag beendet."
        frmGreetings.ShowDialog()
        tbxUserInput.Select()
    Else
        frmNotificationBox.Label1.Text = "Ihren Arbeitstag ist schon beendet!"
        frmNotificationBox.ShowDialog()
        tbxUserInput.Select()
    End If

End Sub