ado是应用层的编程接口,它通过ole db提供的com接口访问数据,它适合于各种客户机/服务器应用系统和基于web的应用,尤其在一些脚本语言中访问数据库操作是ado的主要优势。ado是一套用自动化技术建立起来的对象层次结构,它比其他的一些对象模型如dao(data access object)、rdo(remote data object)等具有更好的灵活性,使用更为方便,并且访问数据的效率更高。sql是强大的数据库操作系统,通过ado和sql语句的配合,我们可以的实现对数据库的一系列操作,例如创建数据库、创建表、创建索引,实现数据库的多重查询、高级查询和数据的汇总等技术。下面通过例程介绍如何通过ado和sql语句的配合实现对数据库的操作。
第一步:通过access创建数据库test.mdb。
第二步:创建单文档工程testado,所有的选项都取默认值。
第三步:com库的初始化
我们可以使用afxoleinit()来初始化com库,这项工作通常在cwinapp::initinstance()的重载函数中完成,请看如下代码:
bool cadotest1app::initinstance()
{
afxoleinit();
......
第四步:用#import指令引入ado类型库
我们在stdafx.h中加入如下语句:
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("eof","adoeof")
这一语句有何作用呢?其最终作用同我们熟悉的#include类似,编译的时候系统会为我们生成msado15.tlh,ado15.tli两个c++头文件来定义ado库。
第五步:在testadoview.h中定义一个指向connection对象的指针:_connectionptr _pconnection;
第六步:添加如下代码:
| void ctestadoview::oninitialupdate() { cview::oninitialupdate(); hresult hr; try { hr = m_pconnection.createinstance("adodb.connection");//创建connection对象 if(succeeded(hr)) { hr = m_pconnection->open("provider=microsoft.jet.oledb.4.0;data source=test.mdb","","",admodeunknown);///连接数据库 ///上面一句中连接字串中的provider是针对access2000环境的,对于access97,需要改为:provider=microsoft.jet.oledb.3.51; } } } catch(_com_error e)///捕捉异常 { cstring errormessage; errormessage.format("连接数据库失败!\r\n错误信息:%s",e.errormessage()); afxmessagebox(errormessage);///显示错误信息 } } |
第七步:在析构函数中关闭connection对象并将其释放,代码如下:
| ctestadoview::~ctestadoview() { m_pconnection->close(); m_pconnection.release(); } |
第八步:添加菜单项"创建数据库表",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onaddtable() { _variant_t recordsaffected; m_pconnection->execute("create table new(id integer,username text,old integer)",&recordsaffected,adcmdtext); } |
运行程序,执行菜单当中的命令"添加表",我们可以发现数据库中已经添加了一个表new,其中的字段有我们定义的字段。
第九步:添加菜单项"删除数据库表",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::ondeletetable() { _variant_t recordsaffected; m_pconnection->execute("drop table new",&recordsaffected,adcmdtext); } |
运行程序,执行菜单当中的命令"删除表",我们可以发现数据库中刚才添加的表new已被删除。
第十步:添加菜单项"添加一列",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onaddcolumn() { _variant_t recordsaffected; m_pconnection->execute("alter table new add newcolumn1 integer",&recordsaffected,adcmdtext); } |
运行程序,执行菜单当中的命令"添加一列",我们可以发现数据库中刚才添加的表new中已添加了一个新列。
第十一步:添加菜单项"删除一列",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onaddcolumn() { _variant_t recordsaffected; m_pconnection->execute("alter table new add newcolumn1 integer",&recordsaffected,adcmdtext); } |
运行程序,执行菜单当中的命令"删除一列",我们可以发现数据库中刚才添加的表new中的新列已被删除。
第十二步:添加菜单项"添加记录",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onaddrecord() { _variant_t recordsaffected; for(int i = 1;i < 10; i ++) { cstring strsql; strsql.format("insert into new(id,username,old) values (%d, 'washington',%d)",i,i*9); m_pconnection->execute((_bstr_t)strsql,&recordsaffected,adcmdtext); } |
运行程序,执行菜单当中的命令"添加记录",我们可以发现数据库中刚才添加的表new中添加了九条新的记录。
第十三步:添加菜单项"old字段加1",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onoldaddone() { _variant_t recordsaffected; m_pconnection->execute("update new set old = old+1",&recordsaffected,adcmdtext); }} |
运行程序,执行菜单当中的命令"old记录加1",我们可以发现数据库中刚才添加的表new中的九条新的记录的old字段都自动加1。
第十四步:添加菜单项"统计记录数目",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::ontotalrecords() { _recordsetptr m_precordset; _variant_t recordsaffected; m_precordset =m_pconnection->execute("select count(*) from new where id > 0",&recordsaffected,adcmdtext); _variant_t vindex = (long)0; _variant_t vcount = m_precordset->getcollect(vindex); ///取得第一个字段的值放入vcount变量 m_precordset->close();///关闭记录集 cstring message; message.format("共有%d条记录",vcount.lval); afxmessagebox(message);///显示当前记录条数 } |
运行程序,执行菜单当中的命令"统计记录数目",我们可以得到数据库中记录的数目。
第十五步:添加菜单项"设置id为索引",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onsetidindex() { _variant_t recordsaffected; m_pconnection->execute("create unique index id on new(id)",&recordsaffected,adcmdtext); } |
运行程序,执行菜单当中的命令"设置id为索引",我们可以发现数据库中id被设置为索引。
第十六步:添加菜单项"数据汇总"、"old字段的总和"、"old字段的均值"、"old的最小值"、"old字段的最大值",并添加相应的消息处理函数,然后添加代码如下:
| void ctestadoview::onoldmax() { _recordsetptr m_precordset; _variant_t recordsaffected; m_precordset =m_pconnection->execute("select max(old) from new",&recordsaffected,adcmdtext); _variant_t vindex = (long)0; _variant_t vcount = m_precordset->getcollect(vindex); ///取得第一个字段的值放入vcount变量 m_precordset->close();///关闭记录集 m_precordset.release(); cstring message; message.format("最大值是%d",vcount.lval); afxmessagebox(message); } void ctestadoview::onoldmin() { _recordsetptr m_precordset; _variant_t recordsaffected; m_precordset =m_pconnection->execute("select min(old) from new",&recordsaffected,adcmdtext); _variant_t vindex = (long)0; _variant_t vcount = m_precordset->getcollect(vindex); ///取得第一个字段的值放入vcount变量 m_precordset->close();///关闭记录集 m_precordset.release(); cstring message; message.format("最小值是%d",vcount.lval); afxmessagebox(message); } void ctestadoview::onoldtotal() { _recordsetptr m_precordset; _variant_t recordsaffected; m_precordset =m_pconnection->execute("select sum(old) from new",&recordsaffected,adcmdtext); _variant_t vindex = (long)0; _variant_t vcount = m_precordset->getcollect(vindex); ///取得第一个字段的值放入vcount变量 m_precordset->close();///关闭记录集 m_precordset.release(); cstring message; message.format("总和是%d",(long)vcount); afxmessagebox(message); } void ctestadoview::onoldaverage() { _recordsetptr m_precordset; _variant_t recordsaffected; m_precordset =m_pconnection->execute("select avg(old) from new",&recordsaffected,adcmdtext); _variant_t vindex = (long)0; _variant_t vcount = m_precordset->getcollect(vindex); ///取得第一个字段的值放入vcount变量 m_precordset->close();///关闭记录集 m_precordset.release(); cstring message; message.format("平均值是%d",(long)vcount); afxmessagebox(message); }} |