程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> VC++ >> ado方式訪問帶密碼數據庫的方法

ado方式訪問帶密碼數據庫的方法

編輯:VC++

 主要代碼:
  
   CString strSql;
   TCHAR FileName[MAX_PATH],errMsg[MAX_PATH];
  
   ::CoInitialize(NULL); //初始化Com
  
   IADORecordBinding *picRs = NULL;
   _RecordsetPtr pRs("ADODB.Recordset");
   _ConnectionPtr pConn("ADODB.Connection" );
   //CFootballTeamRs rsFootballTeam;
   GetModuleFileName(NULL,FileName,MAX_PATH);
   (_tcsrchr(FileName,'\\'))[1] = 0;
  
   lstrcat(FileName,_T("pass.mdb"));
  
   strSql = strSql +
     "Provider=Microsoft.Jet.OLEDB.4.0;" +
   "Data Source = " +
  FileName +
   ";Persist Security Info=False;Jet OLEDB:Database Password=cd;";
  
   //pConn->ConnectionString = chDataSource;
   //pConn->Provider = _T("Microsoft.Jet.OLEDB.4.0");
   CString sqlSentence = _T("select * from pass");
   try
   {
   pConn->Open((_bstr_t)strSql, "", "", adModeUnknown);
  
   pRs->QueryInterface(
   __uuidof(IADORecordBinding), (LPVOID*)&picRs);
  
   pRs->Open( (_variant_t)sqlSentence, // 查詢DemoTable表中所有字段
   pConn.GetInterfacePtr(), // 獲取庫接庫的IDispatch指針
     adOpenDynamic,
   adLockOptimistic,adCmdText);
   }
   catch (_com_error &e)
   {
   sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
   AfxMessageBox(errMsg);
   pRs->Close();
   pConn->Close();
   ::CoUninitialize();
   return;
   }
  
   try
   {
   while(!(pRs->EndOfFile))
   {
   CString pass = (LPCTSTR)(_bstr_t)pRs->GetCollect("pass");
   if(pass.IsEmpty()) return;
   SetDlgItemText(IDC_PASS,pass);
   pRs->MoveNext();
   //break;
   }
   pRs->Close();
   pConn->Close();
   }
   catch (_com_error &e)
   {
   sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
   AfxMessageBox(errMsg);
   pRs->Close();
   pConn->Close();
   ::CoUninitialize();
     return;
   }
    
   CoUninitialize();

  1. 上一頁:
  2. 下一頁:
欄目導航
Copyright © 程式師世界 All Rights Reserved