最近做了一个数据诊断的项目,里面自己写了一个数据库的操作类,包含:连接数据库、读数据表、执行SQL操作,释放数据库等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢。
ADOOperate.H
-
-
-
-
-
- #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.h"
- class CADOOperate
- {
- public:
- CString m_DataSource;
- CString m_PassWord;
- CString m_UserName;
- _ConnectionPtr m_pConn;
- 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
-
-
-
- #include "stdafx.h"
- #include "ADOOperate.h"
- #include "h_Const.h"
- #include "ShareFun.h"
- #include "FileLog.h"
-
-
-
- CADOOperate::CADOOperate()
- {
- strTableName = " ";
- m_DataSource = " ";
- m_PassWord = " ";
- m_UserName = " ";
- }
- CADOOperate::~CADOOperate()
- {
- }
-
-
-
-
-
-
-
-
- BOOL CADOOperate::OpenDataBase(CString strDBType)
- {
- CString strSQL;
- CShareFun myFun;
-
- if (strDBType == 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)
- {
- strSQL = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BookManage" ;
- }
- else if (strDBType == ORACLE)
- {
-
- 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);
-
- 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;
- }
-
-
-
-
-
-
-
-
- _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;
- }
-
-
-
-
-
-
-
-
- void CADOOperate::ExitADO()
- {
- if(m_pRst->State)
- {
- m_pRst->Close();
- }
- m_pRst = NULL;
- if (m_pConn->State)
- {
- m_pConn->Close();
- }
- m_pConn = NULL;
-
-
-
-
- CoUninitialize();
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
- BOOL CADOOperate::funCheckTable(CString strName,CString strDBType)
- {
- CString strSQL;
-
- if (strDBType == ACCESS)
- {
- strSQL = "SELECT * FROM MSysObjects WHERE Name = ‘" + strName + "‘";
- }
- else if (strDBType == SQLSERVER)
- {
- strSQL = " " ;
- }
- else if (strDBType == 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;
- }
一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER,布布扣,bubuko.com
一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER
原文:http://www.cnblogs.com/lvdongjie/p/3754489.html