ADO 数据查询
关键点
上1条 下1条 第1条 最后1条
实现过程
// stdafx.h : include file for standard system include files,
#import "c:program filescommon filessystemadomsado15.dll" no_namespace rename ("EOF", "adoEOF")
// ADODlg.h : header file public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
// ADODlg.cpp : implementation file BOOL CADODlg::OnInitDialog()
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Db1.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库Db1.mdb是否在当前路径下!");
return FALSE;
}
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM student", // 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
void CADODlg::OnFirst()
{
_variant_t var;
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("已经是第一个了");
return;
}
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
m_strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sex");
if(var.vt != VT_NULL)
m_strSex = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
{
m_nAge = atoi(LPCTSTR(_bstr_t(var)));
}
var = m_pRecordset->GetCollect("Grade");
if(var.vt != VT_NULL)
{
m_nGrade = atoi(LPCTSTR(_bstr_t(var)));
}
UpdateData(FALSE);
}
void CADODlg::OnPrev()
{
_variant_t var;
if(m_pRecordset->BOF)
{
AfxMessageBox("已经是最后一个了");
return;
}
m_pRecordset->MovePrevious();
if(m_pRecordset->BOF)
{
AfxMessageBox("已经是第一个了");
return;
}
else
{
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
m_strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sex");
if(var.vt != VT_NULL)
m_strSex = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
{
m_nAge = atoi(LPCTSTR(_bstr_t(var)));
}
var = m_pRecordset->GetCollect("Grade");
if(var.vt != VT_NULL)
{
m_nGrade = atoi(LPCTSTR(_bstr_t(var)));
}
UpdateData(FALSE);
}
}
void CADODlg::OnNext()
{
_variant_t var;
if(m_pRecordset->adoEOF)
{
AfxMessageBox("已经是最后一个了");
return;
}
m_pRecordset->MoveNext();
if(m_pRecordset->adoEOF)
{
AfxMessageBox("已经是最后一个了");
return;
}
else
{
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
m_strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sex");
if(var.vt != VT_NULL)
m_strSex = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
{
m_nAge = atoi(LPCTSTR(_bstr_t(var)));
}
var = m_pRecordset->GetCollect("Grade");
if(var.vt != VT_NULL)
{
m_nGrade = atoi(LPCTSTR(_bstr_t(var)));
}
UpdateData(FALSE);
}
}
//最后1条
void CADODlg::OnLast()
{ _variant_t var;
if(!m_pRecordset->adoEOF)
m_pRecordset->MoveLast();
else
{
AfxMessageBox("已经是最后一个了");
return;
}
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
m_strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sex");
if(var.vt != VT_NULL)
m_strSex = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
{
m_nAge = atoi(LPCTSTR(_bstr_t(var)));
}
var = m_pRecordset->GetCollect("Grade");
if(var.vt != VT_NULL)
{
m_nGrade = atoi(LPCTSTR(_bstr_t(var)));
}
UpdateData(FALSE);
}
|
图
备注
相关链接