AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

ado方式访问带密码数据库的方法

51自学网 2015-08-30 http://www.wanshiok.com
主要代码:
  
   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();

 

 

 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
上一篇:经验总结之防止窗口闪烁的方法  下一篇:显示内存中的HTML内容