/***************************************************************************************************
** 函數名 :RestoreData
** 輸 入 :const CString &desc 目的資料庫路徑
** const CString &source 來源資料庫路徑
** 輸 出:
** 功能描述:將來源資料庫中的內容匯入到目的資料庫中
** 全域變數:
** 調用模組:
** 作 者:劉志永
** 日 期:2008-9-10
** 修 改:
** 日 期:
** 版 本:
***************************************************************************************************/
BOOL CDbRestore::RestoreData(const CString &desc, const CString &source)
{
try
{
//CoInitialize(NULL);
//_ConnectionPtr pconn(__uuidof(Connection));
//_ConnectionPtr pSourceConn(__uuidof(Connection));
//_RecordsetPtr prs(__uuidof(Recordset));
//_CommandPtr m_pCmd(__uuidof(Command));
//int dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,desc,-1,NULL,0,NULL,FALSE);
//char lpszStr[MAX_PATH+1];
//WideCharToMultiByte(CP_OEMCP,NULL,desc,-1,lpszStr,dwMinSize,NULL,FALSE);
//int dwSourceMinSize = WideCharToMultiByte(CP_OEMCP,NULL,source,-1,NULL,0,NULL,FALSE);
//char lpszSourceStr[MAX_PATH+1];
//WideCharToMultiByte(CP_OEMCP,NULL,source,-1,lpszSourceStr,dwSourceMinSize,NULL,FALSE);
CString tmpFrom(source);
CString tmpTo(desc);
char buf[512];
buf[0]='0';
strcpy(buf,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
strcat(buf,tmpTo);
strcat(buf,";Persist Security Info=False");
char bufSource[512];
bufSource[0]='0';
strcpy(bufSource,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
strcat(bufSource,tmpFrom);
strcat(bufSource,";Persist Security Info=False");
pconn->ConnectionString=buf;
pSourceConn->ConnectionString=bufSource;
pconn->Open("","","",adConnectUnspecified);
prs=pconn->OpenSchema(adSchemaTables);
m_pCmd->ActiveConnection = pconn;
pSourceConn->Open("","","",adConnectUnspecified);
while (!prs->EndOfFile)
{
//_bstr_t bstTableType=(_bstr_t)prs->Fields->GetItem("TABLE_TYPE")->Value;
if (!strcmp((_bstr_t)prs->Fields->GetItem("TABLE_TYPE")->Value,"TABLE"))
{
try
{
_bstr_t bstTableName=(_bstr_t)prs->Fields->GetItem("TABLE_NAME")->Value;
char buf[512];
strcpy(buf,"INSERT INTO ");
strcat(buf,bstTableName);
strcat(buf," SELECT * From ");
strcat(buf,bstTableName);
strcat(buf," IN '");
strcat(buf, tmpFrom);
strcat(buf,"'");
_bstr_t bstSql=buf;
m_pCmd->CommandText=bstSql;
m_pCmd->CommandType=adCmdText;
m_pCmd->Execute(NULL,NULL,adCmdText);
}
catch (...)
{
}
prs->MoveNext();
}
else
prs->MoveNext();
}
prs->Close();
pconn->Close();
pSourceConn->Close();
return TRUE;
}
catch (...)
{
return FALSE;
}
}