Article View: borland.public.cppbuilder.database.ado
Article #5348Re: Compact Access Databse
From: Filippo
Date: Thu, 07 Jun 2001 06:44
Date: Thu, 07 Jun 2001 06:44
220 lines
7318 bytes
7318 bytes
This example work with delphi:
- import a msjro.dll library (JRO_TLB type library)
but I can't traslate code in c++ builder
this is the problem:
MyJetEngine := CoJetEngine.Create();
if you succeed to traslate the code
give me the solution.
BYE
FILIPPO
program AccessCompact;
// ***************************************************************************
//
// AccessCompact compacts and repairs Access 97 and Access 2000 databases.
//
// Author: David Simpson (drs@ihug.com.au), 19 Feb 2000
//
// Minor changes: Bob Wasaff (bwasaff@suscom.net), 29 Sep 00 2000
// David Simpson, 30 Sep 2000
//
// ***************************************************************************
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
JRO_TLB; // 'Microsoft Jet and Replication Objects 2.5 Library' or later
// C:\Program Files\Common Files\System\ADO\msjro.dll
procedure CompactDB(const DBname, DBtype: string);
var
MyJetEngine: JetEngine;
strDataSource,
strDataDest,
strDataDestName: string;
begin
if not FileExists(DBname) then
begin
writeln('Error: ''', DBName, ''' not found.');
exit;
end;
// delete any previous temporary mdb file
strDataDestName := ExtractFilePath(DBname) + 'temp.mdb';
if FileExists(StrDataDestName) then
begin
DeleteFile(strDataDestName);
writeln('Previous temporary file ', strDataDestName, ' deleted.');
end;
strDataSource := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DBname + ';';
strDataDest := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + strDataDestName + ';';
// default to Access 2000 format unless 97 format is specified
{ default changed 30 Sep 2000 DRS }
if DBtype = '97' then
// Use Engine Type 4 for Access 97 db
strDataDest := strDataDest + 'Jet OLEDB:Engine Type=4'
else
// Use Engine Type 5 for Access 2000 db
strDataDest := strDataDest + 'Jet OLEDB:Engine Type=5';
MyJetEngine := CoJetEngine.Create();
try
MyJetEngine.CompactDatabase(strDataSource, strDataDest);
writeln(DBname, ' compacted into ', strDataDestName + '.');
MyJetEngine := nil;
if DeleteFile(DBname) then
begin
writeln(DBname, ' deleted.');
if RenameFile(strDataDestName, DBName) then
writeln(strDataDestName, ' renamed ', DBname + '.')
else
writeln('Error: ', strDataDestName, ' could not be renamed ', DBname, '.');
end
else
writeln('Error: ', DBname, ' could not be deleted.');
except
on E: Exception do writeln('Error: ', E.Message);
end
end;
begin
if ParamCount = 0 then
begin
writeln('Error: Enter "ACCESSCOMPACT [/]?" for help.'); {Changed 29 Sep 2000 RVW}
halt;
end;
if (ParamStr(1) = '/?') or {Changed 29 Sep 2000 RVW}
(ParamStr(1) = '?') then
begin
write('AccessCompact compacts and repairs Access 97 and Access 2000 databases.');
writeln(' Compacting also rebuilds the database indexes.');
writeln;
writeln('ACCESSCOMPACT [drive:][path]filename [[/,-]97|2000]'); {Changed 29 Sep 2000 RVW}
writeln;
writeln(' [drive:][path]filename Mandatory. Specifies database to fix.');
writeln(' [97|2000] Database type of 97 or 2000. Default 2000.');
writeln;
write('This utility can be automated by using it from a command (.CMD)');
writeln(' file which is then scheduled with the AT command, or directly');
writeln(' using the NT Control Panel''s scheduler.');
end
else
begin
CoInitialize(nil);
if (ParamCount = 2) then
// default to Access 2000 format unless 97 format selected
if (Paramstr(2) = '-97') or {Changed 29 Sep 2000 RVW}
(Paramstr(2) = '/97') or
(Paramstr(2) = '97') then
CompactDB(ParamStr(1), '97')
else
CompactDB(ParamStr(1), '2000')
else
CompactDB(ParamStr(1), '2000');
end
end.
"Barmaz" <barmxavi@students.hevs.ch> wrote:
>Hello,
>
>I'm trying to compact an Access databse by program code (provider OLEDB
>4.0 ) with ADO. But the following code doesn't work
>
>Can Anybody help telling me what's wrong, Please.
>
>Thanks
>
>-----------------------
>Here the code :
>----------------------
>
>void __fastcall TformMenuPrincipal::FormCloseQuery(TObject *Sender,
> bool &CanClose)
> {
> // bo�te de message demandant confirmation de fermeture
>
> if (Flag) // Pour le bouton quitter on a la demande de confirmation,
>pas pour le logout.
> {
> int res ;
>
> res = MessageBox(NULL, "Voulez-vous r�ellement quitter l'application
>?", "Confirmation de sortie",
> MB_ICONINFORMATION + MB_YESNO + MB_DEFBUTTON1) ;
>
> if (res == IDYES)
> {
> CanClose = true ;
>
>
> DataModuleQry->ConnectionISNetQry->Close() ;
> DataModuleTbl->ConnectionISNetTbl->Close() ;
> DataModuleRecherche->ConnectionISNetRecherche->Close() ;
>
> Variant V ;
> AnsiString dbFile = "C:\\ProjetAGL\\BD\\ISNet.mdb" ;
> AnsiString compactFile = "C:\\ProjetAGL\\BD\\Compact.mdb" ;
>
> // Essai de cr�ation de DBEngine with DAO 4.0
> try
> {
> V = CreateOleObject("DAO.DBEngine.40") ;
> }
> catch(...)
> {
> MessageDlg("Le moteur 4.0 de la base de donn�es n'a pas �t�
>trouv�. Recherche d'un autre.", mtWarning, TMsgDlgButtons() <<
>mbOK, 0) ;
> V = CreateOleObject("DAO.DBEngine") ;
> }
>
> V.Exec(Function("CompactDatabase") << dbFile << compactFile) ;
>
> // Compression
> if (FileExists(compactFile))
> {
> // Remplacement de l'ancienne base par la nouvelle
>compact�e.
> DeleteFile(dbFile) ;
> RenameFile(compactFile, dbFile) ;
> }
> }
> else
> CanClose = false ;
> }
> }
>
>-------------
>End of code
>-------------
>
>*******************************************************
>
>Xavier Barmaz
>Etudiant en Informatique de Gestion
>
>barmxavi@students.hevs.ch
>
>
Technical Details
Message-ID:
<3b1f22ce$1_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3b17f8f0_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 10698b,12a2ec45af4aa183 X-Google-Attributes: gid10698b,public X-Google-ArrivalTime: 2001-06-06 23:49:37 PST From: "Filippo" <bettinagliofil@tiscalinet.it> Sender: "Filippo" <bettinagliofil@tiscalinet.it> Reply-To: "Filippo" <bettinagliofil@tiscalinet.it> Subject: Re: Compact Access Databse Newsgroups: borland.public.cppbuilder.database.ado X-User-Info: 62.122.13.164 62.122.13.164 References: <3b17f8f0_1@dnews> NNTP-Posting-Host: 207.105.83.65 Message-ID: <3b1f22ce$1_1@dnews> Date: 6 Jun 2001 23:44:30 -0700 X-Trace: dnews 991896270 207.105.83.65 (6 Jun 2001 23:44:30 -0700) Lines: 221 Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews