遇到一个问题,使用的是accesss数据库,在加载数据库中的信息的时候,速度特别慢,有时候一次查询最终会卡15秒左右,然后才能得到结果,跟踪发现卡的地方在SQLExecDirect中,已经是odbc api了。打开数据库看也没发现啥问题,后来点了access上边的 “压缩和修复数据库”工具,数据库从原来的 8M 瞬间变成了 800k ,使用压缩后的数据库时速度就特别快了,不知道是不是access数据库的啥毛病,记录下压缩用到的代码:
关键代码:
CDaoWorkspace dao;
//将 Properties.mdb 压缩为 Properties_comp.mdb
dao.CompactDatabase(g_CurrentPath + "Properties.mdb",g_CurrentPath + "Properties_comp.mdb");
压缩之后替换原来的文件,完整代码:
VOID CLoginPasswordDlg::CompactDatabase()
{
try
{
CFileFind finder;
if(finder.FindFile(g_CurrentPath + "Properties_comp.mdb"))
{
finder.Close();
DeleteFile(g_CurrentPath + "Properties_comp.mdb");
}
if(finder.FindFile(g_CurrentPath + "Properties.mdb"))
{
finder.Close();
CFile file;
if(file.Open(g_CurrentPath + "Properties.mdb",CFile::modeReadWrite))
{
file.Close();
CDaoWorkspace dao;
dao.CompactDatabase(g_CurrentPath + "Properties.mdb",g_CurrentPath + "Properties_comp.mdb");
}
}
}
catch(...)
{
OutputDebugString("CompactDatabase exception.");
}
try
{
CFileFind finder;
if(finder.FindFile(g_CurrentPath + "Properties.mdb") && finder.FindFile(g_CurrentPath + "Properties_comp.mdb"))
{
finder.Close();
if(!CopyFileA(g_CurrentPath + "Properties_comp.mdb",g_CurrentPath + "Properties.mdb",FALSE))
OutputDebugString("CopyFile failed.");
DeleteFile(g_CurrentPath + "Properties_comp.mdb");
}
}
catch(...)
{
}
}
需要包含头文件:
#include "afxdao.h"