16. november 2001 - 13:29Der er
9 kommentarer og 2 løsninger
Komprimering af access database
Jeg har lavet et program der bruger en access database via TDatabase, TTabel osv.
Efterhånden som jeg skriver og sletter poster vokser databasen støt og roligt. Der er en funktion i MS-Access der komprimere database. Nu mangler jeg en ligende funktion til mit program. Findes den allerede, skal jeg bruge en anden komponent eller hu??
I dette særtema ser vi på, hvordan cloud og AI bliver fundamentet for virksomhedernes digitale forretning, og hvordan de nye muligheder for automatisering og forretningsværdi kan udnyttes uden at miste overblik, sikkerhed og menneskelig kontrol.
begin QueryCompress := TQuery.Create(Self); with QueryCompress do begin Database := DinDB; DisableControls SQL.Clear SQL.Add(\'DenSQL streng som skal til for at komprimere\'); ExecSql; EnableControls; free; end; end;
You can import the MS JRO type library and use Ole automation to compact an access database
Here\'s a procedure that works for me. You have to import the JRO type library to compact an Access database. Here is some code that has been posted before
procedure TMainForm.CompactDataBaseMenuClick(Sender: TObject); var JROJetEngine: TJROJetEngine; strTempDBPath, strSource, strDest, strJetVersion: string; iLength: integer; begin if strGlobalAccessVersion = \'2000\' then strJetVersion := \'5\' { Access 2000} else if strGlobalAccessVersion = \'97\' then strJetVersion := \'4\'; {Jet 3.x, Access 97} strTempDBPath := ExtractFilePath(strGlobalDBPath) + \'TEMP.MDB\'; strSource := \'Data Source=\' + strGlobalDBPath + \';Jet OLEDB:Engine Type=\' + strJetVersion; strDest := \'Data Source=\' + strTempDBPath + \';Jet OLEDB:Engine Type=\' + strJetVersion; try try Screen.Cursor := crHourGlass; if not dbModule.CloseAll then begin MessageDlg(\'Error closing some datasets, unable to compact\' ,mtError,[mbOk], 0); exit; end else dbModule.ADOConnection1.Close; Application.ProcessMessages; JROJetEngine := TJROJetEngine.Create(Application); JROJetEngine.CompactDatabase(strSource, strDest); SysUtils.DeleteFile(strGlobalDBPath); RenameFile(strTempDBPath, strGlobalDBPath); MessageDlg(\'The database has been packed\', mtInformation, [mbOk], 0); except on E: Exception do MessageDlg(\'Error packing database: \' + E.Message, mtError, [mbOk],0); end; finally JROJetEngine.Free; dbModule.ADOConnection1.Open; Screen.Cursor := crDefault; end; end;
How to compact or repair the access database from within delphi
Answer:
If you works with MS Access database, I sure that time-to-time you needs to compact your database. Of course, very useful to run a some process within own application.
In the next code I demonstrates how you can do it:
var dao: OLEVariant; begin dao := CreateOleObject(\'DAO.DBEngine.35\'); dao.CompactDatabase(\'d:\\yourDatabaseName.mdb\', \'d:\\yourNewCompactedDatabaseName.mdb\'); end;
If you want to remove the old non-compacted database, you can simply to rename the new database file.
The MS Access is not very stable database and in network mode you can lose the data. In this case you can try to repair the database.
In the next code I demonstrates how you can do it:
var dao: OLEVariant; begin dao := CreateOleObject(\'DAO.DBEngine.35\'); dao.RepairDatabase(\'d:\\yourDatabaseName.mdb\'); end;
If you have the DAO 3.6, you must change the \'DAO.DBEngine.35\' string to \'DAO.DBEngine.36\'.
How to compact and repair MS Access 2000 (Jet Engine 4) during run time using Delphi 5? Answer:
We all know the size of MS Access keep growing fast by time because of it internal caching and temporary buffering, which in over whole effect the performance and the space required for backing-up (if needed). The solution is to compact it from Access 2000 menus (Tools – Database Utilities – Compact and Repair Database) or to do that from inside your Delphi application.
if CompactAndRepair(\'e:\\Old.mdb\', \'e:\\New.mdb\') then ShowMessage(\'Successfully\') else ShowMessage(\'Error…\');
Important Notes: 1- Include the JRO_TLB unit in your uses clause. 2- Nobody should use or open the database during compacting. 3- If the compiler gives you an error on the JRO_TLB unit follow these steps: a) Using the Delphi IDE go to Project – Import Type Library. b) Scroll down until you reach “Microsoft Jet and Replication Objects 2.1 Library”. c) Click on Install button. d) Recompile a gain.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.