数据库不会自动更新

时间:2020-03-04 09:37:48

标签: vb.net

Dim day As String = Format(Today, "dddd") 'string format of days
Dim hoursinterval As Integer  'date borrowed 
Dim retrievtime As Date  'date borrowed 
Dim convertedtimeH As Integer    'hour borrowed 
Dim convertedtimeM As Integer   'minute borrowed 

Dim time As Date 'current date
Dim CurrHour As Integer ' current hour
Dim CurrMinute As Integer 'current minute


Public Sub timeOfOvernight(ByVal id As Integer, ByVal day As Integer)
    If day >= 1 Then
        sql = "UPDATE `tblborrow` SET `Due` = 1,`Remarks`='Over Due' WHERE Status='Borrowed' AND `Purpose` ='Overnight' and `BorrowID` in ('" & id & "')"
        updates(sql)
        Return
    End If
End Sub

这是我的更新代码:

Function updates(ByVal sql As String)
        Try
            con.Open()
            cmd = New MySqlCommand
            With cmd
                .Connection = con
                .CommandText = sql
                result = cmd.ExecuteNonQuery
                ' If result = 0 Then
                'MsgBox("No updated data", MsgBoxStyle.Information)
                'Else
                'MsgBox("Data in the database has been updated")
                'End If
            End With
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message & "update")
        End Try
        Return result
End Function

这里的问题是,当我的书到了到期日期时,它不会更新数据库中的查询。.我在笔记本电脑中调整了日期,但仍然没有更新。我对此没有任何错误。这就是为什么我很难解决数据库更新的原因。

可以重建我的代码吗?我是vb.net的新手,我仍在学习,非常感谢您

1 个答案:

答案 0 :(得分:0)

假设您的数据库ID是一个整数(因为这就是将它传递给timeOfOvernight的方式),然后从以下位置更改SQL:

sql = "UPDATE `tblborrow` SET `Due` = 1,`Remarks`='Over Due' WHERE Status='Borrowed' AND `Purpose` ='Overnight' and `BorrowID` in ('" & id & "')"

sql = string.format("UPDATE `tblborrow` SET `Due` = 1,`Remarks`='Over Due' WHERE Status='Borrowed' AND `Purpose` ='Overnight' and `BorrowID` in ({0})",id)

或更好

sql = string.format("UPDATE `tblborrow` SET `Due` = 1,`Remarks`='Over Due' WHERE Status='Borrowed' AND `Purpose` ='Overnight' and `BorrowID` = {0}",id)

您正在将ID括在SQL命令中的引号中,这会将其转换为字符串。删除引号,它应该将其视为整数,然后-希望-找到要更新的内容。至少我是这样认为的,但是值得一试!

这当然假定您在代码中的某个位置正确地调用了timeOfOvernight。从发布的代码中我们看不到。