1,StdAfx.h中加入
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
2,
BOOL C***App::InitInstance()
{
AfxEnableControlContainer();
if(!AfxOleInit())
{
AfxMessageBox("OLE初始化Error");
return FALSE;
}
if (FAILED(pConn.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create Instance failed!");
return FALSE;
}
CString strSRC = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\Test.mdb;Jet OLEDB:Database Password=80";
_bstr_t bstrSRC(strSRC);
if (FAILED(pConn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("Can not open Database!");
pConn.Release();
return FALSE;
}
......
}
void C***Dlg::AccessSql(CString szSql)
{
_bstr_t bstrSql( szSql );
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = pConn;
pCommand->CommandText = bstrSql;
pCommand->CommandType = adCmdText;
pCommand->Parameters->Refresh();
pRs = pCommand->Execute(NULL,NULL,adCmdUnknown);
}
CString C***Dlg::GetStrValue(CString szSql, CString szFeild)
{
_bstr_t bstrFeild( szFeild );
AccessSql( szSql );
if( pRs->adoEOF )
return "";
_variant_t varValue = pRs->GetCollect( bstrFeild );
CString Value = (char*)_bstr_t(varValue);
return Value;
}
double C***Dlg::GetDoubleValue(CString szSql, CString szFeild)
{
_bstr_t bstrFeild( szFeild );
AccessSql( szSql );
if( pRs->adoEOF )
return -1;
_variant_t varValue = pRs->GetCollect( bstrFeild );
double Value = (double)varValue;
return Value;
}
long C***Dlg::GetIntValue(CString szSql, CString szFeild)
{
_bstr_t bstrFeild( szFeild );
AccessSql( szSql );
if( pRs->adoEOF )
return -1;
_variant_t varValue = pRs->GetCollect( bstrFeild );
long Value = (long)varValue;
return Value;
}