我在asp之中看到了数据库压缩的代码,但是转到vb之后,就不行了,请问能实现吗?
谢谢

解决方案 »

  1.   

    http://access911.net/index.asp?board=4&mode=3&recordid=76FABF1E17DC
      

  2.   

    用ADO压缩和修复数据库
    Compact and repair databases in ADOIn a previous tip (Compact and repair Jet databases with DAO), we showed you how to compact and repair a database with DAO. However, if you're using ADO, you can also compact and repair Access databases using Jet and Replication Objects (JRO).
    To take advantage of JRO's CompactDatabase method, which will also repair the database, set a reference the JRO library, Microsoft Jet And Replication Objects 2.1 Library. Next, declare and set a JetEngine object variable, like so
    Dim JRO As JRO.JetEngineSet JRO = New JRO.JetEngine
    JRO.CompactDatabase _"Provider=Microsoft.Jet.OLEDB.4.0;" _& "Data Source=C:\myData1.mdb", _"Provider=Microsoft.Jet.OLEDB.4.0;" _& "Data Source=C:\myDataNewName.mdb"_& ";Jet OLEDB:Engine Type=4"
    This command follows similar syntax as DAO's CompactDatabase method-you must provide the old database name and the new database name. In the case of JRO, however, you must also provide a valid connection string, as well as indicate the type of Jet engine to compact the database as. We used 4 to create a Jet Version 3.x database suitable for Access 97 users. If you work with Access 2000 use the value 5 to create a Jet Version 4.x database.
      

  3.   

    用DAO压缩和修复数据库
    Compact and repair Jet databases with DAOIf you're like most developers, you've run into problems with Microsoft Access databases. Access' databases are typically reliable, but occasionally they can get damaged. There are numerous reasons why, such as powering down the computer without first closing the database file or the system simply crashing. Another source of problems for databases is excessive fragmentation-when records are deleted from a database file, the file becomes fragmented. Whatever the reason, once the database is damaged, end-users are often left stranded until technical support finds the time to help them. Fortunately, you can take measures to help users quickly recover from such problems, by providing built-in utilities that perform routine database maintenance.
    The DAO library provides two methods to perform the necessary maintenance that keeps Access databases running smoothly--RepairDatabase and CompactDatabase. The RepairDatabase method uses the syntax
    RepairDatabase dbname
    where dbname is a string representing the full pathname of the Access database (.MDB) that you need to repair. This CompactDatabase method uses the following syntax:
    CompactDatabase olddb, newdb, _locale, options, password
    You specify the full path and filename of your source database as a string in the method's olddb property. Likewise, you specify the destination of the compacted database with the newdb property.
    So, to use these statements, you could add code similar to:
    RepairDatabase "C:\myData1.mdb"CompactDatabase "C:\myData1.mdb", App.Path & "\myDataNewName.mdb"
    If you use DA0 3.6, then you can remove the RepairDatabase statement. DAO 3.0 incorporates this functionality into the CompactDatabase method-you can no longer compact and repair databases as separate actions.
      

  4.   

    在VB中压缩ACCESS 数据库    如果您在 Access 数据库、Access 项目中删除数据或对象,可能会产生碎片并导致磁盘空间使用效率的降低。同时,数据库文件的大小并未减小,而是不断的增大,直至您的硬盘没有空间。有没有好的处理方法呢?其实,在Access中可以对数据库进行压缩优化以提升Access 数据库和 Access 项目的性能,这样的压缩处理的实质是复制该文件,并重新组织文件在磁盘上的存储方式。但是,在 Access 项目中进行这样的压缩不会影响到数据库对象(例如表或视图),因为它们是存储在 Microsoft SQL Server 数据库中而不是在 Access 项目本身中。同样,这样的压缩也不会影响到 Access 项目中的自动编号。在 Access 数据库中,如果已经从表的末尾删除了记录,压缩该数据库是就会重新设置自动编号值。添加的下一个记录的自动编号值将会比表中没有删除的最后记录的自动编号值大一。 
      下面介绍如何在VB中用一个CompactJetDatabase过程实现对Access数据库文件的压缩处理,在这个过程中有一个可选参数,就是在压缩前你是否需要把原有的数据库文件备份到临时目录(True或 False)。我用此办法使21.6MB的数据库压缩到仅仅300KB。 
      ' 这些代码可放在模块中,在其他窗体也使用 
      Public Declare Function GetTempPath Lib "kernel32" Alias _ 
        "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long 
      Public Const MAX_PATH = 260 
      Public Sub CompactJetDatabase(Location As String, Optional BackupOriginal As Boolean = True) 
      On Error GoTo CompactErr 
      Dim strBackupFile As String 
      Dim strTempFile As String 
      '检查数据库文件是否存在 
      If Len(Dir(Location)) Then 
       ' 如果需要备份就执行备份 
       If BackupOriginal = True Then 
       strBackupFile = GetTemporaryPath & "backup.mdb" 
       If Len(Dir(strBackupFile)) Then Kill strBackupFile 
       FileCopy Location, strBackupFile 
       End If    ' 创建临时文件名 
       strTempFile = GetTemporaryPath & "temp.mdb" 
       If Len(Dir(strTempFile)) Then Kill strTempFile    '通过DBEngine 压缩数据库文件 
       DBEngine.CompactDatabase Location, strTempFile    ' 删除原来的数据库文件 
       Kill Location    ' 拷贝刚刚压缩过临时数据库文件至原来位置 
       FileCopy strTempFile, Location