ado方式访问带密码数据库的方法
发表于:2007-07-01来源:作者:点击数:
标签:
主要代码: CString strSql; TCHAR FileName[MAX_PATH],errMsg[MAX_PATH]; ::CoInitialize(NULL); //初始化Com IADORecordBinding *picRs = NULL; _RecordsetPtr pRs(ADODB.Recordset); _ConnectionPtr pConn(ADODB.Connection ); //CFootballTeamRs rsFootb
主要代码:
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( (_vari
ant_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();
原文转自:http://www.ltesting.net