代码未执行查询

时间:2015-02-03 14:30:55

标签: vb.net

我有这个代码,但是当我运行它时,它没有执行查询,有人可以帮助我吗?

If timenow <= time2 Then
            cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,late)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',0)", cn)
            MsgBox("Time out record success!")
        ElseIf timenow < time1 Then
            MsgBox("your late!")
            cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,absent)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',1)", cn)
            MsgBox("Time in record success!")
            cmd.ExecuteScalar()
        End If

2 个答案:

答案 0 :(得分:0)

好吧,首先我要看一下if语句的第一部分,你没有执行查询。假设你在上面声明了cmd变量,你只需要在if语句之后调用它一次。

    If timenow <= time2 Then
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,late)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',0)", cn)
        MsgBox("Time out record success!")
    ElseIf timenow < time1 Then
        MsgBox("your late!")
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,absent)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',1)", cn)
        MsgBox("Time in record success!")
    End If

cmd.ExecuteScalar()

答案 1 :(得分:0)

这是代码

    If timenow <= time2 Then
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,late)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',0)", cn)
        cn.Open()
        i = cmd.ExecuteNonQuery
        cn.Close()
        If i > 0 Then
            MsgBox("save", MessageBoxIcon.Information, "save")
        Else
            MsgBox("err", MessageBoxIcon.Error, "error")

        End If
    ElseIf timenow < time1 Then
        MsgBox("your late!")
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,absent)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',1)", cn)
        cn.Open()
        i = cmd.ExecuteNonQuery
        cn.Close()
        If i > 0 Then
            MsgBox("save", MessageBoxIcon.Information, "save")
        Else
            MsgBox("err", MessageBoxIcon.Error, "error")
        End If
    End If