工控编程吧
标题: 上位机MFC如何使用ADO打开和关闭记录集 [打印本页]
作者: qq263946146 时间: 2019-8-9 11:06
标题: 上位机MFC如何使用ADO打开和关闭记录集
首先是引入ADO动态库
#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();
再初始化变量
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("数据库打开失败。"));
}
这样就可以使用按钮调用我们定义的函数了。
- void CGkbc8Dlg::OnButton1()
- {
- if (m_pRecordset->State & adStateOpen)
- {
- AfxMessageBox(_T("记录集已经打开。"));
- return;
- }
- CString strSource = _T("SELECT * FROM DemoTable");
- if (OpenRecordset(strSource))
- {
- AfxMessageBox(_T("记录集打开成功。"));
- }
- else
- {
- AfxMessageBox(_T("记录集打开失败。"));
- }
- }
- void CGkbc8Dlg::OnButton2()
- {
- if (!(m_pRecordset->State & adStateOpen))
- {
- AfxMessageBox(_T("记录集未打开。"));
- return;
- }
- if (CloseRecordset())
- {
- AfxMessageBox(_T("记录集关闭成功。"));
- }
- else
- {
- AfxMessageBox(_T("记录集关闭失败。"));
- }
- }
复制代码 下面是我们自定义的4个函数
- 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;
- }
- }
复制代码 最重要的,记得初始化COM库。
- if (!SUCCEEDED(CoInitialize(NULL)))
- {
- ::AfxMessageBox(_T("Failed to initialize COM!"));
- return FALSE;
- }
复制代码
这里分享一个例程
(, 下载次数: 0)