ADO方式操作access数据库

ADO方式访问access数据库,实现函数

1.打开数据库

2.关闭数据库

3.查询

4.执行SQL语句

5.获取ADO记录集记录数量

连接数据库字符串查询 https://www.connectionstrings.com/sql-server-2008/ 

ADOConnectAccess.h

 1 #pragma once
 2 
 3 #pragma warning(disable:4146)
 4 #import "C:/Program Files/Common Files/System/ADO/msado15.dll" named_guids rename("EOF","adoEOF"), rename("BOF","adoBOF")
 5 #pragma warning(default:4146)
 6 using namespace ADODB;
 7 
 8 //调用此类要先 AfxOleInit
 9 class ADOConnectAccess
10 {
11 public:
12     ADOConnectAccess(CString);
13     ~ADOConnectAccess();
14 
15     //初始化—连接数据库
16     BOOL ConnectDatabase();
17     //得到数据库记录集指针
18     _RecordsetPtr& GetRecordSet(_bstr_t);
19     //执行SQL语句
20     BOOL ExecuteSQL(_bstr_t);
21     //执行查询语句 为了查询个数 查询个数用查询语句查不能执行
22     _RecordsetPtr& GetRecordSetExecuteSQL(_bstr_t);
23     //断开连接
24     void DisconnectDatabase();
25 
26     // 定义变量
27 public:
28     //添加一个指向Connection对象的指针:
29     _ConnectionPtr m_pConnection;
30     //添加一个指向Recordset对象的指针:
31     _RecordsetPtr m_pRecordset;
32 
33     //mdb数据库文件全路径
34     CString strMDBPathName;
35 };

ADOConnectAccess.cpp

  1 #include "stdafx.h"
  2 #include "ADOConnectAccess.h"
  3 
  4 
  5 ADOConnectAccess::ADOConnectAccess(CString strMdbPathName)
  6 {
  7     this->strMDBPathName = strMdbPathName;
  8 }
  9 
 10 
 11 ADOConnectAccess::~ADOConnectAccess()
 12 {
 13 }
 14 
 15 //连接数据库
 16 BOOL ADOConnectAccess::ConnectDatabase()
 17 {
 18     try
 19     {
 20         // 创建Connection对象
 21         m_pConnection.CreateInstance("ADODB.Connection");
 22         // 设置连接字符串,必须是BSTR型或者_bstr_t类型
 23         CString sTemp;
 24         sTemp.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), strMDBPathName);
 25         //sTemp = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\wangen\Desktop\test.mdb");
 26         _bstr_t strConnect = sTemp;
 27         m_pConnection->Open(strConnect, "", "", adModeUnknown);
 28         //m_pRecordset->Open(_variant_t(strConnect), NULL, adOpenStatic, adLockOptimistic, adCmdText);
 29         return TRUE;
 30     }
 31     // 捕捉异常
 32     catch (_com_error e)
 33     {
 34         // 显示错误信息
 35         AfxMessageBox(e.Description());
 36         return FALSE;
 37     }
 38 }
 39 
 40 //得到数据库记录集指针
 41 _RecordsetPtr& ADOConnectAccess::GetRecordSet(_bstr_t bstrSQL)
 42 {
 43     try
 44     {
 45         // 连接数据库,如果Connection对象为空,则重新连接数据库
 46         if (m_pConnection == NULL)
 47         {
 48             BOOL res = ConnectDatabase();
 49             if (res)
 50             {
 51                 // 创建记录集对象
 52                 m_pRecordset.CreateInstance(__uuidof(Recordset));
 53                 // 取得表中的记录
 54                 m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), 
 55                     adOpenDynamic, adLockOptimistic, adCmdText);
 56             }
 57             else
 58             {
 59                 m_pRecordset = NULL;
 60             }
 61         }
 62         else
 63         {
 64             // 创建记录集对象
 65             m_pRecordset.CreateInstance(__uuidof(Recordset));
 66             // 取得表中的记录
 67             m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), 
 68                 adOpenDynamic, adLockOptimistic, adCmdText);
 69         }
 70     }
 71     // 捕捉异常
 72     catch (_com_error e)
 73     {
 74         // 显示错误信息
 75         AfxMessageBox(e.Description());
 76         m_pRecordset = NULL;
 77     }
 78     // 返回记录集
 79     return m_pRecordset;
 80 }
 81 
 82 //执行SQL语句
 83 BOOL ADOConnectAccess::ExecuteSQL(_bstr_t bstrSQL)
 84 {
 85     try
 86     {
 87         // 连接数据库,如果Connection对象为空,则重新连接数据库
 88         if (m_pConnection == NULL)
 89         {
 90             BOOL res = ConnectDatabase();
 91             if (res)
 92             {
 93                 m_pConnection->Execute(bstrSQL, NULL, adCmdText);
 94                 return TRUE;
 95             }
 96             else
 97             {
 98                 return FALSE;
 99             }
100         }
101         else
102         {
103             m_pConnection->Execute(bstrSQL, NULL, adCmdText);
104             return TRUE;
105         }
106     }
107     // 捕捉异常
108     catch (_com_error e)
109     {
110         // 显示错误信息
111         AfxMessageBox(e.Description());
112         return FALSE;
113     }
114 }
115 
116 
117 _RecordsetPtr& ADOConnectAccess::GetRecordSetExecuteSQL(_bstr_t bstrSQL)
118 {
119     try
120     {
121         // 连接数据库,如果Connection对象为空,则重新连接数据库
122         if (m_pConnection == NULL)
123         {
124             BOOL res = ConnectDatabase();
125             if (res)
126             {
127                 m_pRecordset = m_pConnection->Execute(bstrSQL, NULL, adCmdText);
128             }
129             else
130             {
131                 m_pRecordset = NULL;
132             }
133         }
134         else
135         {
136             m_pRecordset = m_pConnection->Execute(bstrSQL, NULL, adCmdText);
137         }
138         return m_pRecordset;
139     }
140     // 捕捉异常
141     catch (_com_error e)
142     {
143         // 显示错误信息
144         AfxMessageBox(e.Description());
145         return m_pRecordset;
146     }
147 }
148 
149 
150 //断开连接数据库
151 void ADOConnectAccess::DisconnectDatabase()
152 {
153     // 关闭记录集和连接
154     if (m_pRecordset != NULL)
155     {
156         m_pRecordset->Close();
157         m_pRecordset = NULL;
158     }
159     if (m_pConnection != NULL)
160     {
161         m_pConnection->Close();
162         m_pConnection = NULL;
163     }
164 }

调用代码

 1 void xxxDlg::OnBnClickedOk()
 2 {
 3     // TODO: 在此添加控件通知处理程序代码
 4     //////////////////////////////////////////////////////////////////////////
 5     //连接
 6     CString strPath = GetExeRootPath();
 7     CString strName = _T("test.mdb");
 8     CString strPathName = strPath + strName;
 9 
10     ADOConnectAccess m_access(strPathName);
11     //BOOL res = m_access.ConnectDatabase();
12     //if (res)
13     //{
14     //    MessageBox(_T("数据库连接成功"));
15     //}
16     //else
17     //{
18     //    MessageBox(_T("数据库连接失败"));
19     //}
20     ////////////////////////////////////////////////////////////////////////
21     //查询
22     //CString strSql = _T("select (count)* from qwerasdf;");//table是关键词
23     //_bstr_t bstrSql = strSql;
24     //m_access.GetRecordSet(bstrSql);
25     //FieldsPtr pFields = m_access.m_pRecordset->GetFields();
26     //int nCount = pFields->GetCount();
27     //FieldPtr pField;
28     //CString strTitle, strValue;
29     //_variant_t varValue;
30     //字段名
31     //for (int i = 0; i < nCount; i++)
32     //{
33     //    pField = pFields->GetItem((_variant_t)(long)i);
34     //    strTitle = (LPCSTR)pField->Name;
35     //    MessageBox(strTitle);
36     //}
37     //已知字段名
38     //if (!m_access.m_pRecordset->adoEOF)
39     //{
40     //    int iValue;
41     //    while (!m_access.m_pRecordset->adoEOF)
42     //    {
43     //        //索引方式查询或字段名方式查询
44     //        //varValue = m_access.m_pRecordset->GetCollect(_variant_t((long)0));//这两种方式都可用
45     //        varValue = m_access.m_pRecordset->GetCollect("ID");
46     //        iValue = varValue.intVal;
47     //        strValue.Format(_T("%d"), iValue);
48     //        MessageBox(strValue);
49     //        //varValue = m_access.m_pRecordset->GetCollect(_variant_t((long)1));
50     //        varValue = m_access.m_pRecordset->GetCollect("value1");
51     //        strValue = varValue.bstrVal;
52     //        MessageBox(strValue);
53     //        //varValue = m_access.m_pRecordset->GetCollect(_variant_t((long)2));
54     //        varValue = m_access.m_pRecordset->GetCollect("value2");
55     //        strValue = varValue.bstrVal;
56     //        MessageBox(strValue);
57     //        m_access.m_pRecordset->MoveNext();
58     //    }
59     //}
60 
61     //int res = m_access.m_pRecordset->GetRecordCount();//-1
62     //CString strMsg;
63     //strMsg.Format(_T("%d"), res);
64     //MessageBox(strMsg);
65 
66     //m_access.DisconnectDatabase();
67     //////////////////////////////////////////////////////////////////////////
68     //执行SQL
69     //CString strSql = _T("INSERT INTO qwerasdf (`value1`, `value2`) VALUES ('222', '222')");
70     //bstr_t bStrSql = strSql;
71     //BOOL res = m_access.ExecuteSQL(bStrSql);
72     //if (res)
73     //{
74     //    MessageBox(_T("执行成功"));
75     //}
76     //else
77     //{
78     //    MessageBox(_T("执行失败"));
79     //}
80     //m_access.DisconnectDatabase();
81     //////////////////////////////////////////////////////////////////////////
82     //执行查询语句 查询个数
83     CString strSql = _T("SELECT COUNT(0) FROM qwerasdf");
84     bstr_t bStrSql = strSql;
85     m_access.m_pRecordset = m_access.GetRecordSetExecuteSQL(bStrSql);
86     _variant_t varValue;
87     int iValue;
88     CString strValue;
89     while (!m_access.m_pRecordset->adoEOF)
90     {
91         varValue = m_access.m_pRecordset->GetCollect(_variant_t((long)0));//这两种方式都可用
92         iValue = varValue.intVal;
93         strValue.Format(_T("%d"), iValue);
94         MessageBox(strValue);
95         m_access.m_pRecordset->MoveNext();
96     }
97     m_access.DisconnectDatabase();
98 }

注意事项

ADO记录集获取记录数量

这里的方法我用的是第3种,前2种没编译过

2.

_variant_t((long)0) 这里的long一定不能少,少了编译没问题,运行就全是异常了

3.

strTitle = (LPCSTR)pField->Name;   _bstr_t 转CString, 用format %s 是乱码的

4.字段名的引号是复制的,我也不知道键盘怎么输入 ` 

在Access中执行SQL语句

原文地址:https://www.cnblogs.com/ckrgd/p/14465742.html