跨資料庫表間資料Copy的原始碼

來源:互聯網
上載者:User

/***************************************************************************************************
** 函數名  :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;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.