0

下面的代码运行没有错误,但 MS Access 表没有更新。我错过了什么?

    Try
        cnn = New OleDbConnection(ConfigurationManager.ConnectionStrings("accConnectionString").ToString())
        cnn.Open()
    Catch ex As Exception
        Debug.Print("Oops - no connection to database")
        Exit Sub
    End Try

    Dim sql As String
    sql = "SELECT * FROM tblSend WHERE UploadedSuccessfullyOn is null ORDER BY QueuedOn;"
    Dim da As New OleDbDataAdapter(sql, cnn)
    Dim ds As New DataSet
    da.Fill(ds, "dsQueuedToSend")
    For Each dr As DataRow In ds.Tables("dsQueuedToSend").Rows

    ' Perform other unrelated tasks in this space, removed for brevity

        ' If those other tasks were successful, update Access.
        If success = True Then
            sql = "UPDATE [tblSend] SET [UploadedSuccessfullyOn] = @uploadedOn WHERE (([UploadID]) = @uploadId);"
            Dim cmd As OleDbCommand
            cmd = New OleDbCommand(sql, cnn)
            cmd.Parameters.Add("@uploadedOn", OleDbType.Date).Value = Now
            cmd.Parameters.Add("@uploadedId", OleDbType.Integer).Value = dr("UploadID")
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            Console.WriteLine("Success: updated.")
        Else
            Console.WriteLine("Failed: Not updated.")
        End If
    Next

    da.Dispose()
    cnn.Close()

我也尝试过:

 sql =  "UPDATE [tblSend] SET [UploadedSuccessfullyOn] = ? WHERE (([UploadID]) = ?);"

或者只是在非首选/非参数方法中添加值:

sql =  "UPDATE [tblSend] SET [UploadedSuccessfullyOn] = #" & Now & "# WHERE (([UploadID]) = " & dr("UploadID") & ");"
Dim cmd As OleDbCommand
cmd = New OleDbCommand(sql, cnn)
cmd.ExecuteNonQuery()
cmd.Dispose()

这些方法都不会更新 MS Access 表。想法?谢谢!

4

1 回答 1

1

我之前在使用 Visual Studio 时遇到的一件事是,在 WinForms 项目中嵌入 Access 数据库时,当您运行该项目时,Access 数据库会从原始目录复制到运行时目录,从而覆盖您所做的任何更改.

这可能会造成没有发生更新的错觉,而实际上,如果您在正确的时刻进行检查,就会发生更新。

于 2013-12-28T07:02:17.547 回答