压缩数据库错误

时间:2013-07-05 16:56:50

标签: .net vb.net ms-access-2010

我有一个程序可以删除Access2010数据库中某个日期之前的任何数据,然后压缩数据库。该程序的删除部分工作正常,但我正在尝试紧接它后,我收到错误“无效参数”。这是我的代码:

    'Deleting anything older than chosen before databse is compacted.
    Dim DateA As Date = Date.Now
    Dim DateB As String
    Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Fortedb.accdb"
    Dim ParentCNN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Fortedb.accdb"
    Dim CloneCNN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Temp.accdb"
    Dim cnn As New OleDbConnection(ConnString)
    Dim sql As String

    'Formatting the date to make it 7 days into the past.
    DateB = Format(DateA.AddDays(ApplicationPropertiesWindow.DeleteFilebox.SelectedIndex + 1), "MM/dd/yy")
    cnn.Open()

    'Delete everything from b_forte where proddate is less than date - chosen time.    
    sql = "DELETE * FROM b_forte WHERE ProdDate < #" & DateB & "#"

    Dim Command = New OleDb.OleDbCommand(sql, cnn)
    Command.ExecuteNonQuery()
    cnn.Close()
    'Compacting the databse
    Try

        Dim JrO As New JRO.JetEngine

        cnn.Open()
        JrO.CompactDatabase(ParentCNN, CloneCNN)
        If System.IO.File.Exists("C:\Forte\Temp.accdb") Then
            System.IO.File.Delete("C:\Forte\Fortedb.accdb")
            Rename("C:\Forte\Temp.accdb", "C:\Forte\Fortedb.accdb")
            Logging("Database compacted.")
            cnn.Close()
        End If

    Catch ex As Exception
        MainTextBox.AppendText(Environment.NewLine & "Database Compression Failure :" & vbCr & ex.Message)
    End Try

我正在使用vb.net 2010并访问2010而数据库没有密码。

2 个答案:

答案 0 :(得分:1)

CompactDatabase的参数只是路径和文件名,而不是连接信息。

BTW VB.NET不区分大小写,所以我不会重复使用JRO:

Dim JrO As New JRO.JetEngine

它可能会按原样运行,但稍后阅读将会令人困惑,并且可能会在某个阶段引起冲突。

答案 1 :(得分:0)

我从@Andy G中发现这是来自CompactDatabase的连接信息。我必须改变我的代码的信息是:CloneCNN。该字符串为"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Temp.accdb" ,并修复了无效参数错误消息,我将;Jet OLEDB:Engine Type=5添加到CloneCNN的末尾。