1. BOOL ADOConn::OnInitADOConn()
{
// 初始化OLE/COM库环境
if( !m_bCoInitialize)
{
if( S_OK!= ::CoInitialize(NULL))
{
AfxMessageBox(_T("初始化COM库错误!"));
}
}
try
{
// 创建Connection对象
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
if( SUCCEEDED( hr))
{
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect = "Provider=SQLOLEDB; Server=(local); Database=001; Integrated Security=SSPI; uid=test; pwd=12345;";
hr = m_pConnection->Open(strConnect,"","",adModeUnknown);
if( SUCCEEDED( hr))
{
return true;
}
}
else
{
AfxMessageBox(_T("创建Connection对象失败!"));
}
}
catch(_com_error e) // 捕捉异常
{
// 显示错误信息
AfxMessageBox(e.Description());
}
return false;
}
2. int ADOConn::GetVehicleNum()
{
int nRet = 0;
int nInto = 0;
int nOut = 0;
if( NULL != m_pConnection)
{
// 创建车辆入场记录集对象
if( NULL == m_pRecordsetInto)
{
m_pRecordsetInto.CreateInstance(__uuidof(Recordset));
}
HRESULT hr = m_pRecordsetInto->Open(_T("SELECT IC卡号码 FROM 车辆入场"), _variant_t((IDispatch *)m_pConnection, true),
adOpenStatic, adLockReadOnly, adCmdText);
if( SUCCEEDED( hr))
{
// 得到车辆入场记录条数
nInto = m_pRecordsetInto->RecordCount;
}
// 创建车辆出场记录集对象
if( NULL == m_pRecordsetOut)
{
m_pRecordsetOut.CreateInstance(__uuidof(Recordset));
}
hr = m_pRecordsetOut->Open(_T("SELECT IC卡号码 FROM 车辆出场"), _variant_t((IDispatch *)m_pConnection, true),
adOpenStatic, adLockReadOnly, adCmdText);
if( SUCCEEDED( hr))
{
// 得到车辆出场记录条数
nOut = m_pRecordsetOut->RecordCount;
}
}
nRet = nInto - nOut;
return nRet;
}
{
// 初始化OLE/COM库环境
if( !m_bCoInitialize)
{
if( S_OK!= ::CoInitialize(NULL))
{
AfxMessageBox(_T("初始化COM库错误!"));
}
}
try
{
// 创建Connection对象
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
if( SUCCEEDED( hr))
{
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect = "Provider=SQLOLEDB; Server=(local); Database=001; Integrated Security=SSPI; uid=test; pwd=12345;";
hr = m_pConnection->Open(strConnect,"","",adModeUnknown);
if( SUCCEEDED( hr))
{
return true;
}
}
else
{
AfxMessageBox(_T("创建Connection对象失败!"));
}
}
catch(_com_error e) // 捕捉异常
{
// 显示错误信息
AfxMessageBox(e.Description());
}
return false;
}
2. int ADOConn::GetVehicleNum()
{
int nRet = 0;
int nInto = 0;
int nOut = 0;
if( NULL != m_pConnection)
{
// 创建车辆入场记录集对象
if( NULL == m_pRecordsetInto)
{
m_pRecordsetInto.CreateInstance(__uuidof(Recordset));
}
HRESULT hr = m_pRecordsetInto->Open(_T("SELECT IC卡号码 FROM 车辆入场"), _variant_t((IDispatch *)m_pConnection, true),
adOpenStatic, adLockReadOnly, adCmdText);
if( SUCCEEDED( hr))
{
// 得到车辆入场记录条数
nInto = m_pRecordsetInto->RecordCount;
}
// 创建车辆出场记录集对象
if( NULL == m_pRecordsetOut)
{
m_pRecordsetOut.CreateInstance(__uuidof(Recordset));
}
hr = m_pRecordsetOut->Open(_T("SELECT IC卡号码 FROM 车辆出场"), _variant_t((IDispatch *)m_pConnection, true),
adOpenStatic, adLockReadOnly, adCmdText);
if( SUCCEEDED( hr))
{
// 得到车辆出场记录条数
nOut = m_pRecordsetOut->RecordCount;
}
}
nRet = nInto - nOut;
return nRet;
}