跨数据库Copy的一个类的代码 希望对大家能有所帮助
#include "StdAfx.h" #include "./dbcopyclass.h" CDbCopyClass::CDbCopyClass(void) { ::CoInitialize(NULL); pconn.CreateInstance(__uuidof(Connection)); pSourceConn.CreateInstance(__uuidof(Connection)); prs.CreateInstance(__uuidof(Recordset)); m_pCmd.CreateInstance(__uuidof(Command)); m_SourceRs.CreateInstance(__uuidof(Recordset)); m_DesRs.CreateInstance(__uuidof(Recordset)); /// m_fields.CreateInstance(__uuidof(Fields)); //m_fieldPtr.CreateInstance(__uuidof(FieldPtr)); } CDbCopyClass::~CDbCopyClass(void) { prs.Release(); m_SourceRs.Release(); m_DesRs.Release(); pconn.Release(); pSourceConn.Release(); m_pCmd.Release(); ::CoUninitialize(); } /*************************************************************************************************** ** 函数名 :RestoreData ** 输 入 :const CString &desc 目的数据库路径 ** const CString &source 源数据库路径 ** 输 出: ** 功能描述:将源数据库中的内容导入到目的数据库中 ** 全局变量: ** 调用模块: ** 作 者:刘志永 ** 日 期:2008-9-10 ** 修 改: ** 日 期: ** 版 本: ***************************************************************************************************/ BOOL CDbCopyClass::RestoreData(const CString &desc, const CString &source) { try { 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); pSourceConn->Open("","","",adConnectUnspecified); prs=pSourceConn->OpenSchema(adSchemaTables); m_pCmd->ActiveConnection = pconn; while (!prs->adoEOF) { if (!strcmp((_bstr_t)prs->Fields->GetItem("TABLE_TYPE")->Value,"TABLE")) { _bstr_t bstTableName=(_bstr_t)prs->Fields->GetItem("TABLE_NAME")->Value; if (m_SourceRs->GetState()==1) { m_SourceRs->Close(); } if (m_DesRs->GetState()==1) { m_DesRs->Close(); } if (bstTableName==_bstr_t("User")) { bstTableName=_bstr_t("[User]"); } _bstr_t strSqlDel="Delete From "+bstTableName; //m_pCmd. m_pCmd->CommandText=strSqlDel; m_pCmd->CommandType=adCmdText; m_pCmd->Execute(NULL,NULL,adCmdText); char buf[512]; buf[0]='0'; strcpy(buf," SELECT * From "); strcat(buf,bstTableName); m_SourceRs->CursorType = adOpenStatic; m_SourceRs->CursorLocation = adUseClient; m_DesRs->CursorType = adOpenStatic; m_DesRs->CursorLocation = adUseClient; m_SourceRs->Open(buf,_variant_t((IDispatch*)pSourceConn,TRUE),adOpenStatic,adLockOptimistic,adCmdUnknown); m_DesRs->Open(buf,_variant_t((IDispatch*)pconn,TRUE),adOpenStatic,adLockOptimistic,adCmdUnknown); int nFieldsCount=m_SourceRs->Fields->Count; while (!m_SourceRs->adoEOF) { m_DesRs->AddNew(); for (int nLoop=0;nLoop<nFieldsCount;nLoop++) { _variant_t vtIndex; vtIndex.vt = VT_I2; vtIndex.iVal = nLoop; _variant_t tFieldValue=m_SourceRs->Fields->GetItem(vtIndex)->Value; m_DesRs->Fields->GetItem(vtIndex)->put_Value(tFieldValue); } m_DesRs->Update(); m_SourceRs->MoveNext(); } prs->MoveNext(); } else prs->MoveNext(); } prs->Close(); pconn->Close(); pSourceConn->Close(); return TRUE; } catch (...) { return FALSE; } }