CSDN博客

img taianmonkey

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

发表于2004/4/26 12:39:00  777人阅读

分类: 数据库

主要代码:

 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();

0 0

相关博文

我的热门文章

img
取 消
img