首页 > 解决方案 > 访问 2007 紧凑型数据库

问题描述

对不起我的英语,我从意大利写信。我在 VB Net 的业务数据管理程序中使用 Access 2007 DB。众所周知,Access DB 在使用过程中会扩展,因此我需要在程序关闭时压缩它们。我以这种方式执行压缩

Dim MioEngine As New Microsoft.Office.Interop.Access.Dao.DBEngine

MioEngine.CompactDatabase(myAccesDB, newAccessDB,)
Application.DoEvents()

但通常而不是在网络上的所有 PC 上,压缩不会完成并给我以下错误消息: 进程无法访问“C:\myAccesDB.accdb”文件,因为它正在被另一个进程使用 深入分析什么发生时,我看到发生错误时,.laccdb 文件在压缩时没有关闭。除了我用来安全压实的方法之外,还有其他方法吗?我指定我们网络中的所有 PC 都是 Windows 10 专业版并且全部更新;当 Windows 10 处于起步阶段时,我没有收到此错误。

标签: vb.net

解决方案


由于我的评论还不够,这里有一个答案:

这个想法是在.laccdb尝试压缩数据库之前尝试打开文件。你可以这样做:

Dim file as System.IO.FileInfo = New System.IO.FileInfo("c:\myAccesDB.laccdb")
Dim stream As FileStream = Nothing
Try
    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
    stream.Close()
    'if stream is successfully closed, file is not in use and database can be compacted
    MioEngine.CompactDatabase(myAccesDB, newAccessDB,)
Catch ex As Exception
    'show some message to user that file is in use so it can try again
End Try

我很久以前就使用过 VB 和 Access,所以这只是你应该采取的方向。


推荐阅读