从MS Access中删除所有记录

时间:2014-03-08 06:27:52

标签: vb.net ms-access datetime

我正在使用带有名称为TIME作为日期/时间类型的列的1个ms访问,格式为dd / mm / yyy hh:nn:ss

例如

TIME 06/03/2014 23:43:24

现在使用vb.net我已经从网上获得了这些代码以将数据插入表Table1

Dim TrDate As DateTime = DateTime.Now
Dim CurrentDate = Format(TrDate, "yyyy/MM/dd hh:mm:ss tt")


Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick

    dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & FilePath)
    Try
        dbconn.Open()
        strSQL = "INSERT INTO Table1 ([TIME]) VALUES (?)"
        objcmd = New OleDbCommand(strSQL, dbconn)

        objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@TIME", CurrentDate))
        objcmd.ExecuteNonQuery()

        dbconn.Close()
    Catch myException As System.Exception
        Windows.Forms.MessageBox.Show(myException.Message)
    End Try
End Sub

//此查询正常

  Private Sub Timer8_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer8.Tick
    dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & FilePath)
    Try
        Dim TrDate1 As Date = Date.Now
        Dim CurrentDate1 = Format(TrDate1, "yyyy/MM/dd hh:mm:ss tt")
        dbconn.Open()
        strSQL = "DELETE FROM Table1 WHERE TIME < CurrentDate1"
        objcmd = New OleDbCommand(strSQL, dbconn)
        objcmd.ExecuteNonQuery()

        dbconn.Close()
    Catch myException As System.Exception
        Windows.Forms.MessageBox.Show(myException.Message)
    End Try
End Sub

//删除查询并不执行,给出错误“没有为eor //更多参数赋予值”

我希望从今天前一天删除所有记录。我希望每天只运行一次TIMER8

1 个答案:

答案 0 :(得分:2)

此行中有错误

strSQL = "DELETE FROM Table1 WHERE TIME < CurrentDate1"

您必须将CurrentDate1变量本身连接到查询,如下所示:

strSQL = "DELETE FROM Table1 WHERE TIME < " & CurrentDate1

但是,构建SQL语句的推荐方法是使用参数化SQL语句,就像您在第一段代码中所做的那样,而不是使用字符串连接。

strSQL = "DELETE FROM Table1 WHERE TIME < ?"
objcmd = New OleDbCommand(strSQL, dbconn)
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@TIME", CurrentDate1))
objcmd.ExecuteNonQuery()