📄 dbfitter.cpp
字号:
// DBFitter.cpp: implementation of the CDBFitter class.
// Auther :
// Hamed M.
// Contact:
// HamedMosavi@gmail.com
// HamedMosavi@hotmail.com
//
// License: You are free to Use, Modify, Copy, this
// program or it's source code. Since this
// program is licensed free of charge, there
// is no waranty for the program or its source code.
//
// or as others say: This program is provided AS IS
// WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
// IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
// QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
//
// Disclaimer: I coppied the above lines(capital letter) from one of
// those open-source licenses, it is hard to understand
// them to me!
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DBFitter.h"
#import "D:\Program Files\Common Files\system\ado\msado15.dll" rename( "EOF", "MSADO_EOF" )
#import "D:\PROGRAM FILES\COMMON FILES\System\ado\MSJRO.DLL" no_namespace
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDBFitter::CDBFitter()
{
m_szLastError = "No Error.";
}
CDBFitter::~CDBFitter()
{
}
// Gets a string as apath like 'C:\Hi.txt' and returns file name like: 'Hi.txt'
CString CDBFitter::GetFileName(CString szFullPath)
{
if (szFullPath.GetLength()==0) {
return "";
}
if (szFullPath.Right(0)=='\\' ) {
return "";
}
int nFindex = szFullPath.ReverseFind('\\');
if ( nFindex==-1 ) {
return "";
}
return szFullPath.Right(szFullPath.GetLength() - nFindex -1);
}
// Compacts the database file with the path provided with 'szSrcDbPath'
// and copies the compacted file to 'szDstDbPath' the source database
// might or might not have 'szDbPassword' password, but destination
// file will have it.
// The source file will remain UNCOMPACTED, and UNREPAIRED.
BOOL CDBFitter::CompactAndRepair(CString szSrcDbPath, CString szDstDbPath, CString szDbPassword)
{
if (szSrcDbPath==szDstDbPath) {
AfxMessageBox("Source and destination can't be the same,\r\n Please use another form of this function with 2 inputs.");
return FALSE;
}
CString szDbCnn = "", // To hold database connection string
szDbTmpCnn = ""; // To hold temporary database connection string
try
{
szDbCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" // Define engine & version
"Data Source="+ szSrcDbPath + ";"; // Define mdb file path
if (szDbPassword.GetLength()>0) { // If password exists, add it
szDbCnn += ";Jet OLEDB:Database Password=" + szDbPassword;
}
szDbTmpCnn = "Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=" + szDstDbPath + ";"
"Jet OLEDB:Engine Type=5;";
if (szDbPassword.GetLength()>0) {
szDbTmpCnn += ";Jet OLEDB:Database Password=" + szDbPassword;
}
CoInitialize ( NULL ); // This is for COM, I don't understand it actually!
IJetEnginePtr jet(__uuidof(JetEngine)); // Create a pointer to JetEngine interface
BSTR // 2 BSTR variable for the function input
bszCnn = szDbCnn.AllocSysString(),
bszTmpCnn = szDbTmpCnn.AllocSysString();
jet->CompactDatabase(bszCnn, bszTmpCnn);// Do compact and repair database and -
// create a (compacted) copy.
SysFreeString(bszCnn); // Free system allocated memory
SysFreeString(bszTmpCnn);
CoUninitialize();
} catch(_com_error &e) {
// TODO: (( WARNING ))
// The following might lead to memory leak
// UNDONE:
// Call SysFreeString() , if exception happpend
// after AllocSysString
m_szLastError = (LPCTSTR)e.Description( );
return FALSE;
}
// catch(...) {
// UNDONE: Handle rest of the exceptions
// }
return TRUE;
}
// Compacts the database file with the path provided with 'szDbPath'
// The database 'szDbPath' might or might not have 'szDbPassword'
// password, but destination file will have it just if you provide a
// password as function input.
// A file with the name 'acs_tmp_db_cmpct__rpr.mdb' will be created
// as a temporary file, In the same directory that database exsists
// So there must be enough room for both files and if there is a file
// with this name already, will be deleted.
BOOL CDBFitter::CompactAndRepair(CString szDbPath, CString szDbPassword)
{
BOOL res;
CString szTmpDbPath = ""; // To hold the path of the temporary created file
szTmpDbPath = szDbPath; // Create a path for a temporary file, just
// beside the original file
// Remove file name to get the folder path
szTmpDbPath.
Replace(GetFileName(szDbPath),NULL);
// A long file name, hoping there is not such
// a name already in the folder, or it will be
// overwritten
szTmpDbPath += "acs_tmp_db_cmpct__rpr.mdb";
DeleteFile(szTmpDbPath); // Delete temporary file, Make sure the file does
// NOT exsists before starting the process.
res = CompactAndRepair(szDbPath, szTmpDbPath, szDbPassword);
if (FileExists(szTmpDbPath)) {
DeleteFile(szDbPath); // Delete source file
MoveFile(szTmpDbPath, szDbPath); // Rename temp file to source file
} else {
res = FALSE;
m_szLastError = "Could not find temporary"
"file. Main file remaind unchanged";
}
return res;
}
// Compacts the database file with the path provided with 'szDbPath'
// The database 'szDbPath' should not have any password,
// A file with the name 'acs_tmp_db_cmpct__rpr.mdb' will be created
// as a temporary file, In the same directory that database exsists
// So there must be enough room for both files and if there is a file
// with this name already, it will be deleted.
BOOL CDBFitter::CompactAndRepair(CString szDbPath)
{
return CompactAndRepair(szDbPath, "");
}
CString CDBFitter::GetLastErrString()
{
return m_szLastError;
}
BOOL CDBFitter::FileExists(CString szPath)
{
CFile f;
CFileStatus fs;
if (!f.GetStatus(szPath,fs)) {
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -