工控编程吧
标题: 上位机MFC如何使用ADO在数据库中访问记录 [打印本页]
作者: qq263946146 时间: 2019-8-9 11:55
标题: 上位机MFC如何使用ADO在数据库中访问记录
和前面例程一样,也是初始化COM库,加载ODA库。 if (!SUCCEEDED(CoInitialize(NULL)))
{
::AfxMessageBox(_T("Failed to initialize COM!"));
return FALSE;
}
#import "c:\program files\common files\system\ado\msado15.dll"\
no_namespace\
rename("EOF", "adoEOF")
添加自定义数据库变量与函数
- _ConnectionPtr m_pConnection;
- _RecordsetPtr m_pRecordset;
- BOOL OpenDatabase(LPCTSTR lpszConnect, long nOptions=adConnectUnspecified);
- BOOL CloseDatabase();
- BOOL OpenRecordset(LPCTSTR lpszSource, long nCursorType=adOpenDynamic,
- long nLockType=adLockOptimistic, long nOptions=adCmdText);
- BOOL CloseRecordset();
复制代码
- BOOL CGkbc8Dlg::OpenDatabase(LPCTSTR lpszConnect, long nOptions)
- {
- ASSERT(m_pConnection != NULL);
- ASSERT(lpszConnect != NULL);
- ASSERT(AfxIsValidString(lpszConnect));
-
- //打开数据库连接
- try
- {
- return SUCCEEDED(m_pConnection->Open(_bstr_t(lpszConnect),
- _T(""), _T(""), nOptions));
- }
- catch (_com_error& e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return FALSE;
- }
- }
- BOOL CGkbc8Dlg::CloseDatabase()
- {
- ASSERT(m_pConnection != NULL);
- //关闭数据库连接
- try
- {
- if (m_pConnection->State & adStateOpen)
- {
- return SUCCEEDED(m_pConnection->Close());
- }
- else
- {
- return TRUE;
- }
- }
- catch (_com_error& e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return FALSE;
- }
- }
- BOOL CGkbc8Dlg::OpenRecordset(LPCTSTR lpszSource, long nCursorType,
- long nLockType, long nOptions)
- {
- ASSERT(m_pConnection != NULL);
- ASSERT(m_pRecordset != NULL);
- ASSERT(lpszSource != NULL);
- ASSERT(AfxIsValidString(lpszSource));
- //打开记录集
- try
- {
- return (SUCCEEDED(m_pRecordset->Open(_variant_t(lpszSource),
- m_pConnection.GetInterfacePtr(),
- (CursorTypeEnum)nCursorType,
- (LockTypeEnum)nLockType,
- nOptions)));
- }
- catch(_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return FALSE;
- }
- }
- BOOL CGkbc8Dlg::CloseRecordset()
- {
- ASSERT(m_pRecordset != NULL);
- //关闭记录集
- try
- {
- if (m_pRecordset->State & adStateOpen)
- {
- return SUCCEEDED(m_pRecordset->Close());
- }
- else
- {
- return TRUE;
- }
- }
- catch (_com_error e)
- {
- TRACE(_T("%s\n"), e.ErrorMessage());
- return FALSE;
- }
- }
复制代码 再添加控件,一并初始化数据库与控件
- if (!SUCCEEDED(m_pConnection.CreateInstance(__uuidof(Connection))))
- {
- m_pConnection = NULL;
- TRACE(_T("Database CreateInstance failed"));
- }
- if (!SUCCEEDED(m_pRecordset.CreateInstance(__uuidof(Recordset))))
- {
- m_pRecordset = NULL;
- TRACE(_T("Recordset CreateInstance Failed!"));
- }
- //打开数据库
- CString strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= Demo.mdb");
- if (!OpenDatabase(strConnect))
- {
- AfxMessageBox(_T("数据库打开失败。"));
- return false;
- }
- //打开记录集
- CString strSource = _T("SELECT * FROM DemoTable");
- if (!OpenRecordset(strSource))
- {
- AfxMessageBox(_T("记录集打开失败。"));
- return false;
- }
- //初始化ListCtrl
- CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
- pListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
- pListCtrl->InsertColumn(1, _T("序号"), LVCFMT_CENTER, 80);
- pListCtrl->InsertColumn(2, _T("姓名"), LVCFMT_CENTER, 100);
- pListCtrl->InsertColumn(3, _T("年龄"), LVCFMT_CENTER, 100);
复制代码
(, 下载次数: 2)