备份还原Access数据库类

来源:互联网 发布:淘宝客服哪里找 编辑:程序博客网 时间:2024/05/23 17:32
******************************* MDB_IO.h *****************************************************
#ifndef MDB_IOH
#define MDB_IOH

#include <ADODB.hpp>
#include <DB.hpp>

class TDB
{
    void __fastcall RunSQL(const String strSQL, TADOQuery *AQ);
    void __fastcall SelectSQL(const String strSQL, TADOQuery *AQ);
public:
    bool __fastcall MDBSave(char* pathOld, char* pathSave);
    bool __fastcall MDBBack(char* pathOld, char* pathBack, const int Num, char * alTables[]);
};

extern TDB DB;
//---------------------------------------------------------------------------
#endif

******************************* MDB_IO.CPP*****************************************************
#pragma hdrstop

#include "MDB_IO.h"
#include "windows.h"
#include <vcl.h>

//---------------------------------------------------------------------------
#pragma package(smart_init)
TDB DB;

void __fastcall TDB::RunSQL(const String strSQL, TADOQuery *AQ)
{
    try{
        AQ->Close();
        AQ->SQL->Text = strSQL;
        AQ->ExecSQL();
    }catch(...){

    }
}
//---------------------------------------------------------------------------
void __fastcall TDB::SelectSQL(const String strSQL, TADOQuery *AQ)
{
    try{
        AQ->Close();
        AQ->SQL->Text = strSQL;
        AQ->Open();
    }catch(...){

    }
}
//---------------------------------------------------------------------------
bool __fastcall TDB::MDBSave(char* pathOld, char* pathSave)
{
    bool    b_Save = false;

    if(CopyFile(pathOld,pathSave,true))
        b_Save = true;

    return  b_Save;
}
//---------------------------------------------------------------------------
/* 调用 函数
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    char *Tabel[] = {"documents","systemConfig","queueList","playLogs"};
    DB.MDBBack("D://Test//SystemDB.mdb","D://Test//DB.mdb",4,Tabel);
}
*/
bool __fastcall TDB::MDBBack(char* pathOld, char* pathBack, const int Num, char * alTables[])
{
    bool    b_Back = false;
    String strSQL;
    TADOQuery *AQ_1, *AQ_2;

    // 关联数据库
    AQ_1 = new TADOQuery(NULL);
    AQ_1->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source="
        + String(pathOld) +";Persist Security Info=False";
    AQ_1->ParamCheck = false;

    AQ_2 = new TADOQuery(NULL);
    AQ_2->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source="
        + String(pathBack) +";Persist Security Info=False";
    AQ_2->ParamCheck = false;


    for(int i=0; i<Num; i++){
        //*****************************************************************
        /*                        方法一
        // 先从原数据库中删除选中的表
        strSQL = "drop table " + String(alTables[i]);
        RunSQL(strSQL,AQ_1);
        AQ_1->Close();

        // 再从备份数据库中将选中的表恢复到原数据库
        // select * into [;database=D://Test//BroadcastSystemDB.mdb].documents from documents
        strSQL = "select * into [;database=" + String(pathOld) + "]." + String(alTables[i])
                + " from " +String(alTables[i]);
        SelectSQL(strSQL,AQ_2);
        */
        //*****************************************************************

        //*****************************************************************
        //                          方法二
        strSQL = "DELETE FROM " + String(alTables[i]);
        RunSQL(strSQL,AQ_1);

        strSQL = "INSERT INTO ["+ String(pathOld)+  "]." + String(alTables[i])
                + " SELECT * FROM " + String(alTables[i]) ;
        RunSQL(strSQL,AQ_2);
        //*****************************************************************
    }

    delete AQ_1;
    delete AQ_2;

    return  b_Back;
}
/*
老蔡   15:56:24
       我以前的一些代码
      quExec->CommandText = "DELETE FROM DYMain";
      quExec->Execute();
      quExec->CommandText = "INSERT INTO DYMain SELECT * FROM ["+FileName+
                            "].DYMain ORDER BY Id";
      quExec->Execute();
老蔡   15:56:33
quExec是adocommand
老蔡   15:56:51
FileName是个access.mdb文件名
*/
原创粉丝点击