vc中如何使用ado,在线等待
热门软件下载:
我在vc的对话框中添加了ado控件,
1、怎样用代码建立ado与access库jet连接.
2、怎样实现数据的添加,删除,查询。能否给出简单代码。
3、数据处理完后,怎样做退出处理。
推荐阅读
http://www.csdn.net/develop/read_article.asp?id=16163
www.vckbase.com上有文章和例子
http://www.vckbase.com/document/listdoc.asp?mclsid=11&sclsid=1105
一个简单的ado类
/////////////////////////////////////////////////////////
// 注意:使用的时候在app文件初始化的时候加入下面代码:
//
// if (!afxoleinit())
// {
// afxmessagebox("ole 初始化错误!",mb_iconerror);
// return false;
// }
//
// 有时需要(win95 or win98下)在使用时加入:
//
// coinitialize(null);
// couninitialize();
//
#ifndef __my_operate_lib__
#define __my_operate_lib__
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("eof","adoeof")
#include "icrsint.h"
class newadoclass
{
//attributs
public:
_connectionptr m_pconnection;
_recordsetptr m_precordset;
_commandptr m_pcommand;
//operators
public:
//**************** 功能1:连接数据库 ******************
//*******************************************************
// 注释:该函数用于创建一个ado的数据库连接。使用时可以按*
// 照下面的例子进行: *
// newadoclass conn; *
// conn.connectdb(); *
// 这样就建立了数据库的连接,可以进行其他操作。 *
//*******************************************************
bool connectdb()
{
hresult hr;
try
{
hr = m_pconnection.createinstance("adodb.connection");///创建connection对象
if(succeeded(hr))
{
m_pconnection->commandtimeout = 25;
cstring str;
//数据源名称
str.format("nestledb");
hr=m_pconnection->open((_bstr_t)str,"","",admodeunknown);
}
}
catch(_com_error e)///捕捉异常
{
cstring errormessage;
errormessage.format("连接数据库失败!错误信息:%s",e.errormessage());
//afxmessagebox(errormessage);///显示错误信息
newadoclass conn;
conn.writelog(errormessage);
return false;
}
return true;
}
//*********************** end ***************************
//**************** 功能2:操作数据库 ******************
//*******************************************************
// 注释:该函数用于对一个数据库连接执行sql语句。使用时可*
// 以按照下面的例子进行: *
// conn.executesql("select * from table"); *
// 或者 *
// cstring strsql="select * from table"; *
// conn.executesql((_bstr_t)(lpctstr)strsql); *
// 通过该函数可以使用sql语句对数据库进行查询、增加*
// 删除、修改等多种操作。 *
// 在查询时,查询结果保存在conn.m_pconnection中。 *
//*******************************************************
bool executesql(lptstr strsql)
{
_variant_t vnull;
m_pcommand.createinstance("adodb.command");
vnull.vt = vt_error;
vnull.scode = disp_e_paramnotfound;///定义为无参数
m_pcommand->activeconnection = m_pconnection;///非常关键的一句,将建立的连接赋值给它
m_pcommand->commandtext = strsql;///命令字串
cstring strid;
try
{
m_precordset = m_pcommand->execute(&vnull,&vnull,adcmdtext);///执行命令,取得记录集
}
catch(_com_error e)///捕捉异常
{
cstring errormessage;
errormessage.format("打开表失败!错误信息:%s",e.errormessage());
//afxmessagebox(errormessage);///显示错误信息
newadoclass conn;
conn.writelog(errormessage);
return false;
}
return true;
}
//*********************** end ***************************
//************* 功能3:得到当前系统时间 *************
//*******************************************************
// 注释:该函数取得使用于sql server中的时间。 //
// 使用示例: //
// cstring time; //
// conn.getsystime(time) //
// 在写数据库是写入time即可。 //
//*******************************************************
void getsystime(cstring &strtime)
{
ctime time = ctime::getcurrenttime();
int year,month,day/*,hour,minute,second*/;
year = time.getyear();
day = time.getday();
month = time.getmonth();
// hour = time.gethour();
// minute = time.getminute();
// second = time.getsecond();
cstring m_time;
m_time.format("%d-%d-%d",year,month,day);
strtime = m_time;
}
//*********************** end ***************************
//************ 功能4:向日志中写错误信息 ************
//*******************************************************
// 注释:该函数写错误日志记录。 //
// 使用示例: //
// cstring errormessage; //
// conn.writelog(errormessage); //
//*******************************************************
void writelog(cstring &strinfo)
{
cstdiofile file;
cstring str,m_time;
newadoclass conn;
conn.getsystime(m_time);
if(file.open("c:\\logfile",cfile::modereadwrite))
{
file.seektoend();
str.format("%s|%s|\n",strinfo,m_time);
file.writestring(str);
file.close();
}
else
{
return;
}
}
};
#endif
http://www.vckbase.com/vckbase/vckbase10/src/adotest1.zip
http://www.vckbase.com/vckbase/vckbase10/src
很经典的文章和示例!
相关评论