最近做了 一个 数据诊断的项目,里面自己写了 一个 数据库 的 操作 类,包含:连接 数据库 、读数据表、执行sql 操作 ,释放 数据库 等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢。 adooperate.h ///////////////////////
最近做了一个数据诊断的项目,里面自己写了一个数据库的操作类,包含:连接数据库、读数据表、执行sql操作,释放数据库等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢。
adooperate.h
//////////////////////////////////////////////////////////////////////// 类功能:用于数据库的操作 主要实现 连接数据库 读数据表 检查数据表 执行sql语句//// 孙高朝 2010.03.25//////////////////////////////////////////////////////////////////////#if !defined(afx_adooperate_h__eb4ac016_15d4_46e9_a754_e1c1a036daae__included_)#define afx_adooperate_h__eb4ac016_15d4_46e9_a754_e1c1a036daae__included_#if _msc_ver > 1000#pragma once#endif // _msc_ver > 1000#include stdafx.hclass cadooperate { public: cstring m_datasource; // 数据源 cstring m_password; // 密码 cstring m_username; // 数据库名 _connectionptr m_pconn; // ado连接 cstring strtablename; // 表名 外边传入 _recordsetptr m_prst; // 记录集public: bool funchecktable(cstring strname,cstring strdbtype); bool executesql(cstring strsql,lpcstr strdbtype = oracle); _recordsetptr& readtable(lpcstr strsql1 = null,lpcstr strdbtype = oracle); // 读表 返回记录集 bool opendatabase(cstring lpdbtype); // 连接数据库 void exitado(); cadooperate(); virtual ~cadooperate();};#endif // !defined(afx_adooperate_h__eb4ac016_15d4_46e9_a754_e1c1a036daae__included_)
adooperate.c
// adooperate.cpp: implementation of the cadooperate class.////////////////////////////////////////////////////////////////////////#include stdafx.h#include adooperate.h#include h_const.h#include sharefun.h#include filelog.h//////////////////////////////////////////////////////////////////////// construction/destruction//////////////////////////////////////////////////////////////////////cadooperate::cadooperate(){ strtablename = ; // 初始化表名 m_datasource = ; // 数据源 m_password = ; // 密码 m_username = ; // 数据库名}cadooperate::~cadooperate(){}//*********************************************//// 函数功能: 连接数据库// 参数: cstring 字符串// 返回值: true: 连接成功 false:连接失败// 作者: 孙高朝 2010.03.24////*********************************************bool cadooperate::opendatabase(cstring strdbtype){ cstring strsql; csharefun myfun; // 选择数据库连接语句 if (strdbtype == access) // access { cstring strpath; strpath = myfun.fungetpath(); strsql = provider=microsoft.jet.oledb.4.0;data source= + strpath + mobiledb.mdb;persist security info=false; } else if (strdbtype == sqlserver) // sql server { strsql = provider=sqloledb.1;integrated security=sspi;persist security info=false;initial catalog=bookmanage ; } else if (strdbtype == oracle) // 默认为oracle { // 读取ado.ini的文件 m_datasource = myfun.funreadini(datasource); m_username = myfun.funreadini(username); m_password = myfun.funreadini(password); strsql = provider=msdaora.1;password= + (cstring)m_password + ;user id= + (cstring)m_username + ;data source= + (cstring)m_datasource + ;persist security info=true; // 数据库连接语句 } coinitialize(null); // 初始化com库 try { m_pconn.createinstance(__uuidof(connection)); // 连接指针 m_pconn->connectionstring = (_bstr_t)strsql; m_pconn->open(,,,null); } catch (_com_error) // 连接出错处理 { messagebox(0,strdbtype + 连接数据库错误,提示,mb_ok|mb_iconinformation); return false; } catch (...) { afxmessagebox(sys error); return false; } return true;}//*********************************************//// 函数功能: 读取数据库表// 参数: cstring 字符串// 返回值: 返回记录集数// 作者: 孙高朝 2010.03.24////*********************************************_recordsetptr& cadooperate::readtable(lpcstr strsql1,lpcstr strdbtype){ cstring strsql; cstring strsql2; cfilelog myfile; strsql2 = strsql1; strsql = select * from + strtablename ; if(strsql2.getlength() > 0) { strsql = select * from + strtablename + where + strsql1; } try { if(m_pconn==null) // 判断连接是否断开 opendatabase(strdbtype); // 重新连接 m_prst.createinstance(__uuidof(recordset)); // 数据集指针 m_prst->raw_close(); // 关闭记录集 m_prst->open((_bstr_t)strsql,m_pconn.getinterfaceptr(),adopendynamic,adlockoptimistic,adcmdtext); } catch(_com_error e) { myfile.writetolog(errorfilename,cadooperate readtable(),(lpcstr)e.description()); } return m_prst; // 返回记录集}//*********************************************//// 函数功能: ado释放函数// 参数: // 返回值: // 作者: 孙高朝 2010.03.29////*********************************************void cadooperate::exitado(){ if(m_prst->state) { m_prst->close(); } m_prst = null; if (m_pconn->state) { m_pconn->close(); } m_pconn = null; // 错误说明:智能指针_recordsetptr和_connectionptr在超出作用域是会自动释放的, // 也就是说指针在createinstance()的时候分配内存,在作用域外释放, // 所以自己调用release()并不是必需的,不过调用有可能出错,而设为null是完全可以不用的。 // m_pconn->release(); couninitialize(); // 卸载com库}//*********************************************//// 函数功能: 执行数据库操作// 参数: lpcstr 字符串指针// 返回值: 返回记录集数// 作者: 孙高朝 2010.03.29////*********************************************bool cadooperate::executesql(cstring strsql,lpcstr strdbtype){ try { if(m_pconn==null) // 判断连接是否断开 opendatabase(strdbtype); // 重新连接 m_prst.createinstance(__uuidof(recordset)); // 数据集指针 m_prst->raw_close(); // 关闭记录集 m_prst->open((_bstr_t)strsql,m_pconn.getinterfaceptr(),adopendynamic,adlockoptimistic,adcmdtext); } catch(_com_error e) { e.description(); return false; } return true; // 返回记录集}//*********************************************//// 函数功能: 检查数据库是否存在该表// 参数: lpcstr 字符串指针// 返回值: 返回true or false// 作者: 孙高朝 2010.03.29////*********************************************bool cadooperate::funchecktable(cstring strname,cstring strdbtype){ cstring strsql; // 选择数据库连接语句 if (strdbtype == access) // access { strsql = select * from msysobjects where name = ' + strname + '; // 表名强制为大写, 判断是否存在 } else if (strdbtype == sqlserver) // sql server { strsql = ; } else if (strdbtype == oracle) // oracle { strsql = select table_name from tabs where table_name=' + strname + '; // 表名强制为大写, 判断是否存在 } try { if(m_pconn==null) // 判断连接是否断开 opendatabase(oracle); // 重新连接 m_prst.createinstance(__uuidof(recordset)); // 数据集指针 m_prst->raw_close(); // 关闭记录集 m_prst->open((_bstr_t)strsql,m_pconn.getinterfaceptr(),adopendynamic,adlockoptimistic,adcmdtext); } catch(_com_error e) { e.description(); return false; } // 存在表否 if (m_prst->adoeof) { return false; } return true;}