2013-06-26 42 views
1

我想壓縮我的Microsoft Access 2010數據庫。我正在使用VS 2010.我似乎無法讓壓縮工作。我嘗試了幾種方法,並收到不同的錯誤消息。這是我現在的代碼。壓縮數據庫

Private Sub Compactdb() 

    Dim JRO As JRO.JetEngine 
    JRO = New JRO.JetEngine 

    'The first source is the original, the second is the compacted database under an other name. 
    JRO.CompactDatabase("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\C:\Forte\Fortedb.accdb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\C:\Forte\Compactdb.accdb") 


    'Original (not compacted database is deleted) 
    System.IO.File.Delete("C:\Program Files\VSoft\AppMiss\NewAppDB.mdb") 

    'Compacted database is renamed to the original databas's name. 
    Rename("C:\Forte\Compactdb.accdb", "C:\Forte\Fortedb.accdb") 

    'User notification 
    MsgBox("The database was compacted successfully") 

End Sub 

現在我得到的錯誤是

錯誤1無法複製文件「\ phipnasw01 \用戶臀圍$ \ cerns1 \我的文檔\ Visual Studio 2010的\項目\復地數據採集器\復Data Gatherer \ Example1.accdb「更改爲」bin \ Debug \ Example1.accdb「。無法找到文件'\ phipnasw01 \ users-hip $ \ cerns1 \ My Documents \ Visual Studio 2010 \ Projects \ Forte Data Gatherer \ Forte Data Gatherer \ Example1.accdb'。 Forte Data Gatherer

+0

錯誤消息指的是與您的示例不同的文件和路徑。你會在哪一行發生異常? – Steve

+0

這不是特定的行,它只是在Visual Studio底部的錯誤列表中。 –

+0

你有你的accdb文件列在你的項目文件和它的屬性「複製到輸出目錄」設置爲「始終複製」? – Steve

回答

1

當VS DEBUG/RELEASE會話啓動時,IDE會嘗試將該文件從項目文件夾複製到輸出目錄(通常是BIN \ DEBUG)。由於某些原因,IDE無法找到文件或路徑,因此無法將其複製到輸出目錄。

這不是一個編程錯誤,而是您的項目文件的配置。

此副本似乎已經無關,與你所示的代碼,因此,你可以在屬性Copy to Output directory設置爲Never Copy

相反,關於你上面的代碼,你已經做了一些錯誤與文件名

Private Sub Compactdb() 

    Dim JRO As JRO.JetEngine 
    JRO = New JRO.JetEngine 

    Dim source = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Fortedb.accdb" 
    Dim compact = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Compactdb.accdb" 
    JRO.CompactDatabase(source, compact) 

    'Original (not compacted database is deleted) 
    System.IO.File.Delete("C:\Forte\Fortedb.accdb") 

    'Compacted database is renamed to the original databas's name. 
    File.Move("C:\Forte\Compactdb.accdb", "C:\Forte\Fortedb.accdb") 

    'User notification 
    MsgBox("The database was compacted successfully") 

End Sub