工控编程吧
标题: 上位机MFC如何使用ADO在数据库中移动记录集 [打印本页]
作者: qq263946146 时间: 2019-8-9 11:33
标题: 上位机MFC如何使用ADO在数据库中移动记录集
1.初始化COM库
if (!SUCCEEDED(CoInitialize(NULL)))
{
::AfxMessageBox(_T("Failed to initialize COM!"));
return FALSE;
}
2.导入ADO库
#import "c:\program files\common files\system\ado\msado15.dll"\
no_namespace\
rename("EOF", "adoEOF")
3.定义变量与自定义操作数据库的函数
BOOL OpenDatabase(LPCTSTR lpszConnect, long nOptions=adConnectUnspecified);
BOOL CloseDatabase();
BOOL OpenRecordset(LPCTSTR lpszSource, long nCursorType=adOpenDynamic, long nLockType=adLockOptimistic, long nOptions=adCmdText);
BOOL CloseRecorset();
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
最后 就是使用按钮来操作数据库
如前移,后移等
- void CGkbc8Dlg::OnMovefirst()
- {
- if (!(m_pRecordset->State & adStateOpen))
- {
- AfxMessageBox((_T("记录集未打开。")));
- return;
- }
- //判断记录集是否为空
- if (m_pRecordset->BOF && m_pRecordset->adoEOF)
- {
- return;
- }
-
- //移动记录集
- try
- {
- m_pRecordset->MoveFirst();
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return;
- }
-
- //读取记录集
- _variant_t varValue;
- CString strName = _T("");
- int nAge = 0;
- try
- {
- varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
- if (varValue.vt != VT_NULL)
- {
- strName = varValue.bstrVal;
- }
- else
- {
- strName = _T("");
- }
-
- varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE")));
- if (varValue.vt != VT_NULL)
- {
- nAge = varValue.intVal;
- }
- else
- {
- nAge = 0;
- }
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return;
- }
-
- //刷新
- SetDlgItemText(IDC_NAME, strName);
- SetDlgItemInt(IDC_AGE, nAge);
- }
- void CGkbc8Dlg::OnMovelast()
- {
- if (!(m_pRecordset->State & adStateOpen))
- {
- AfxMessageBox((_T("记录集未打开。")));
- return;
- }
- //判断记录集是否为空
- if (m_pRecordset->BOF && m_pRecordset->adoEOF)
- {
- return;
- }
-
- //移动记录集
- try
- {
- m_pRecordset->MoveLast();
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return;
- }
-
- //读取记录集
- _variant_t varValue;
- CString strName = _T("");
- int nAge = 0;
- try
- {
- varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
- if (varValue.vt != VT_NULL)
- {
- strName = varValue.bstrVal;
- }
- else
- {
- strName = _T("");
- }
-
- varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE")));
- if (varValue.vt != VT_NULL)
- {
- nAge = varValue.intVal;
- }
- else
- {
- nAge = 0;
- }
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return;
- }
- //刷新
- SetDlgItemText(IDC_NAME, strName);
- SetDlgItemInt(IDC_AGE, nAge);
- }
- void CGkbc8Dlg::OnMovenext()
- {
- if (!(m_pRecordset->State & adStateOpen))
- {
- AfxMessageBox((_T("记录集未打开。")));
- return;
- }
- //判断记录集是否为空
- if (m_pRecordset->BOF && m_pRecordset->adoEOF)
- {
- return;
- }
- //移动记录集
- try
- {
- m_pRecordset->MoveNext();
- if (m_pRecordset->adoEOF)
- {
- m_pRecordset->MoveFirst();
- }
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return;
- }
- //读取记录集
- _variant_t varValue;
- CString strName = _T("");
- int nAge = 0;
- try
- {
- varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
- if (varValue.vt != VT_NULL)
- {
- strName = varValue.bstrVal;
- }
- else
- {
- strName = _T("");
- }
-
- varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE")));
- if (varValue.vt != VT_NULL)
- {
- nAge = varValue.intVal;
- }
- else
- {
- nAge = 0;
- }
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return;
- }
-
- //刷新
- SetDlgItemText(IDC_NAME, strName);
- SetDlgItemInt(IDC_AGE, nAge);
- }
复制代码
(, 下载次数: 1)