这段时间使用VC++6.0,在写一些通过PLC控制工业硬件设备的机电光一体化的控制程序。现在程序写的差不多了,但是客户的要求需要增加对硬件设备各种信息的存储、查询、统计等功能。在VC++6.0下,还从来没有应用过ADO来处理数据库,在网络上搜罗了一些相关的资料,问题是解决了。在此,把我的一些VC++6.0对ADO的操作感受写一下。当然,对我最主要的也是一个技术备份。
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:黑体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimHei; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@黑体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} h1 {mso-style-next:正文; margin-top:17.0pt; margin-right:0cm; margin-bottom:16.5pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:240%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:1; font-size:22.0pt; font-family:"Times New Roman"; mso-font-kerning:22.0pt;} h2 {mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:Arial; mso-fareast-font-family:黑体; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->
1 ADO 简介
ADO ( ActiveX Data Object ), ActiveX 数据对象,它建立在 OLE DB 之上。 ADO 是一个 OLE DB 用户程序。使用 ADO 在应用程序都要间接地使用 OLD DB 。 ADO 简化了 OLE DB ,提供了对自动化的支持,使用像 VBScript 或 JavaScript 这样的脚本语言也能够使用 ADO 访问数据库。
2 ADO 的三个核心对象
2.1 Connection 对象
Connection 对象表示和数据库的链接,它管理应用程序和数据库之间的通信。 Recordset 和 Command 对象都有一个 ActiveConnection 属性,该属性用来引用 Connection 对象。
2.2 Command 对象
Command 对象被用来处理重复执行的查询,或处理需要检查在存储过程调用中的输出或返回参数的值的查询。
2.3 Recordset 对象
Recordset 对象被用来获取数据。 Recordset 对象存放查询的结果,这些结果由数据的行(记录)和列(字段)组成。每一列都存放在 Recordset 的 Fields 集合中的一个 Field 对象中。
3 进入正题:用 VC6 链接 ACCESS 数据库
3.1 系统 msado15.dll 动态库的应用
将 msado15.dll 动态库拷贝到您的应用程序所在的文件夹下。
msado15.dll 一般放在系统盘目录下,例如: C:/Program Files/Common Files/System/ado 目录下。
在创建的应用程序的“ StdAfx.h ”头文件中加上 #import “msado15.dll” no_namespace rename(“EOF”,”adoEOF”) ,我是如下这样添加的:
#import "msado15.dll" /
no_namespace /
rename ("EOF", "adoEOF")
为什么这样添加?因为记录集的结尾是 EOF ,而文件的结尾也是 EOF ,所以这里为了避免冲突,将其重命名为 adoEOF 。
添加好后,编译程序,这时在您的 Debug 目录下多出现两个文件: msado15.tlh 和 msado15.tli ,这是 msado15.dll 在编译时自动生成的,有时候可以帮助您了解一些函数、参数的含义。
3.2 初始化 ADO 并链接数据库
ADO 实际上是一个 COM 组件,所以我们在使用它之前要进行初始化,结束后要卸载。
首先,定义一个 _ConnectionPtr m_pConnection ( // 链接数据库额职能指针)成员变量。
程序:
// 初始化 ---- 链接数据库
//1 初始化 OLE/COM 库环境
::CoInitialize(NULL);
……
//2 实例化链接对象
hr=m_pConnection.CreateInstance(_uuidof(Connection));
……
//3 链接数据库
m_pConnection->ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source= 数据库名称 ”;
注意:如果 Access 数据库中设置了密码,那么链接字符串应改为: Provider=Microsoft.Jet.OLEDB.4.0;Data Source= 数据库名称 ;Persist Security Info=False;Jet OLEDB:Database Password= 密码
……
//4 设置等待链接打开的时间:例如 5 秒
m_pConnection->ConnectionTimeout = 5;
//5 打开链接
try
{
hr = m_pConnection->Open("","","",adConnectUnspecified);
}
catch (_com_error e) {
AfxMessageBox(" 数据库链接失败,确认数据库 Demo.mdb 是否在当前路径下 !");
return false;
}
3.3 查询
1 、确定 SQL 语句:
CString m_strSQL = "select * from DemoTalbe";
2 、定义 Recordset 对象指针,并实例化 Recordset 对象指针
_RecordsetPtr pQueryRecordset;
hr = pQueryRecordset.CreateInstance(__uuidof(Recordset));
3 、使用 Recordset 对象指针,打开数据库,并执行指定 SQL 语句
try
{
pQueryRecordset->Open(_variant_t(m_SQL),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch (_com_error *e) {
AfxMessageBox(e->ErrorMessage());
return false;
}
4 、检查记录集是否有数据后,获取查询数据到数据表实体对象结构体数组
if ((pQueryRecordset->BOF) && (pQueryRecordset->adoEOF))
{
AfxMessageBox(" 没有数据 !");
return false;
} Else {
while (!pQueryRecordset->adoEOF)
{
// 主键字段
var = pQueryRecordset->GetCollect("ID");
if (var.vt !=VT_NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
DemoTableArray[m_SelectNum].m_ID = _ttol((LPCTSTR)strValue);
}
//StrainID 字段
var = pQueryRecordset->GetCollect("StrainID");
if (var.vt != VT_NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
//DemoTableArray[m_SelectNum].m_StrainID = (CString)strValue;
}
//Resistance 字段
var = pQueryRecordset->GetCollect("Resistance");
if (var.vt != VT_NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
DemoTableArray[m_SelectNum].m_Resistance = atof(strValue);
}
pQueryRecordset->MoveNext();
m_SelectNum++;
}
3.4 修改
Recordset->PutCollect("visit", (LPCTSTR)visit );
Recordset->Update();
3.5 增加
Recordset->AddNew();
Recordset->PutCollect("visit", (LPCTSTR)visit );
Recordset->Update();
3.6 删除
strSQL=”delete from stock”;
Recordset->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
3.7 撤消数据库连接 , 卸载 ADO
m_pConnection->Close(); // 释放环境
::CoUninitialize();