在qt里实现access数据库的compact和repair

增加2个#import,
#import "C:Program FilesCommon FilesSystemadomsado27.tlb" no_namespace rename("EOF","adoEOF")
#import "C:Program FilesCommon FilesSystemadomsjro.dll"
增加从QString到BSTR的转换函数
static inline BSTR QStringToBSTR(const QString &str)
{
 return SysAllocStringLen((OLECHAR*)str.unicode(), str.length());
以下主要代码,注意在srcConnection和desConnection不能指向同一个数据源。
//compact the db
 JRO::IJetEnginePtr jet(__uuidof(JRO::JetEngine));
 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 QString srcConnection=QString("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%1;")
  .arg(dbqPath);
 QString desConnection=QString("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%1;")
  .arg(dbqPath+".tmp");
 jet->CompactDatabase(QStringToBSTR(srcConnection), QStringToBSTR(desConnection));
 bool renameError=false;
 if ( !QFile::rename(dbqPath, dbqPath+"k")) renameError=true;
  
 if (!QFile::rename(dbqPath+".tmp", dbqPath)) renameError=true;
    QApplication::restoreOverrideCursor();

Leave a Reply

Your email address will not be published.