#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include <direct.h>
#include <io.h>
using namespace std;
/*
Filepath :数据库文件路径,不包括文件名例如:Filepath="..\\"
SrcFileName :源文件名
*/
static bool compressMDB(const std::string& Filepath,const std::string &SrcFileName)
{
string backupfilepath;//备份文件夹路径
backupfilepath=Filepath;
backupfilepath+="BackUp";
string srcpath=Filepath;//源文件路径,包括源文件
srcpath+=SrcFileName;
string backup=backupfilepath;//源文件备份路径,包括源文件
backup+="\\";
backup+=SrcFileName;
#if 0
if(CopyFile(backup.c_str(),srcpath.c_str(),FALSE))
{
cout<<"还原成功"<<endl;
}
else
{
cout<<"还原失败"<<endl;
}
#endif
#if 1
/*判断BackUp文件夹是否存在,不存在创建文件夹,存在清空文件夹*/
if(!access(backupfilepath.c_str(),0))
{
/*存在,清空文件夹*/
string DelFile=backupfilepath;
DelFile+="\\";
DelFile+=SrcFileName;
remove(DelFile.c_str());
}
else
{
/*不存在*/
mkdir(backupfilepath.c_str());
}
/*备份源文件*/
if(CopyFile(srcpath.c_str(),backup.c_str(),FALSE))
{
cout<<"备份成功"<<endl;
}
else
{
cout<<"备份失败"<<endl;
}
/*删除原文件*/
if(!remove(srcpath.c_str()))
{
cout<<"删除原文件成功"<<endl;
}
CoInitialize(NULL);
try
{
string strSourceConnection;
strSourceConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strSourceConnection += backup;
strSourceConnection += ";Jet OLEDB:Engine Type=5;";
string strDestConnection;
strDestConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strDestConnection +=srcpath;
strDestConnection += ";Jet OLEDB:Engine Type=5;";
IJetEnginePtr jet(__uuidof(JetEngine));
jet->CompactDatabase(strSourceConnection.c_str(), strDestConnection.c_str());
}
catch(...)
{
if(CopyFile(backup.c_str(),srcpath.c_str(),FALSE))
{
cout<<"还原成功"<<endl;
}
else
{
cout<<"还原失败"<<endl;
}
CoUninitialize();
return false;
}
CoUninitialize();
#endif
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
string path="..\\DB\\";
string sz="Transaction.mdb";
if(compressMDB(path,sz))
{
cout<<"压缩成功"<<endl;
}
else
{
cout<<"压缩失败"<<endl;
}
getchar();
return 0;
}利用ADO压缩Access数据库代码
最新推荐文章于 2015-01-19 11:43:00 发布
本文介绍了一个用于压缩Microsoft Access数据库(MDB格式)的C++程序实现。该程序首先将源数据库文件备份到指定目录下,并尝试删除原文件,然后利用OLE DB接口进行数据库文件的压缩操作。若过程中遇到错误,则会尝试从备份恢复。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Langchain-Chatchat
AI应用
Langchain
Langchain-Chatchat 是一个基于 ChatGLM 等大语言模型和 Langchain 应用框架实现的开源项目,旨在构建一个可以离线部署的本地知识库问答系统。它通过检索增强生成 (RAG) 的方法,让用户能够以自然语言与本地文件、数据库或搜索引擎进行交互,并支持多种大模型和向量数据库的集成,以及提供 WebUI 和 API 服务
695

被折叠的 条评论
为什么被折叠?



