通过vb.net更新MS Access

时间:2013-08-08 01:56:24

标签: vb.net ms-access oledb

以下代码运行没有错误,但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表。想法?谢谢!

1 个答案:

答案 0 :(得分:1)

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

这可能会造成更新没有发生的错觉,事实上如果你在适当的时候检查,那就是。