大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 程序开发 -> VC中利用ADO共同实现数据库的操作

VC中利用ADO共同实现数据库的操作

时间: 2021-07-31 作者:daque

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); }}   运路途序,实行菜单傍边的汇总吩咐,咱们不妨获得关系的汇总消息。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.