/***************************************************************************************************标签:源代码,bufSource,strcat,表间,prs,bstr,NULL,buf,Copy From: https://blog.51cto.com/u_15942605/6024363
** 函数名 :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;
}
}