• ADO数据库编程详解(C++)----初级入门篇


    一、概述

            ADO即Microsoft ActiveXData Object,是Microsoft继ODBC之后,基于OLE DB技术的一种数据库操作技术,使您能够编写通过 OLE DB提供者对在数据库服务器中的数据进行访问和操作的应用程序。ADO同OLE DB、数据库之间的关系可以用下图来表示:

    其主要优点是易于使用、高速度、低内存支出和占用磁盘空间较少。ADO支持用于建立基于客户端/服务器和 Web的应用程序的主要功能。

    二、ADO初步认识

            在此介绍的ADO编程技术都是基于C++的,要熟练掌握如何用ADO操作数据库,首先对ADO技术得有个总体的了解,AD0主要由几个对象组成:Connection、Command、Paramenter、Recorderset、Fields、Error、Property对象,对数据库的操作,都是通过这几个对象来进行的,对象模型的关系图如下:

    • Connection对象

    在数据库应用里操作数据源都必须通过该对象,这是数据交换的环境。Connection对象代表了同数据源的一个会话,在客户/服务器模型里,这个会话相当于同服务器的一次网络连接。不同的数据提供者提供的该对象的集合、方法和属性不同。

    借助于Connection对象的集合、方法和属性,可以使用Open和Close方法建立和释放一个数据源连接。使用Execute方法可以执行一个数据操作命令,使用BeginTrans、CommitTrans和RollbackTrans方法可以启动、提交和回滚一个处理事务。通过操作the Errors 集合可以获取和处理错误信息,操作CommandTimeout属性可以设置连接的溢出时间,操作ConnectionString属性可以设置连接的字符串,操作Mode属性可以设置连接的模式,操作Provider属性可以指定OLE DB提供者。

    • Command对象

    Command对象是一个对数据源执行命令的定义,使用该对象可以查询数据库并返回一个Recordset对象,可以执行一个批量的数据操作,可以操作数据库的结构。不同的数据提供者提供的该对象的集合、方法和属性不同。

    借助于Command对象的集合、方法和属性,可以使用Parameters集合制定命令的参数,可以使用Execute方法执行一个查询并将查询结果返回到一个Recordset对象里,操作CommandText属性可以为该对象指定一个命令的文本,操作CommandType属性可以指定命令的类型,操作Prepared可以得知数据提供者是否准备好命令的执行,操作CommandTimeout属性可以设置命令执行的溢出时间。

    • Parameter对象

    Parameter对象在Command对象中用于指定参数化查询或者存储过程的参数。大多数数据提供者支持参数化命令,这些命令往往是已经定义好了的,只是在执行过程中调整参数的内容。

    借助于Parameter对象的集合、方法和属性,可以通过设置Name属性指定参数的名称,通过设置Value属性可以指定参数的值,通过设置Attributes和Direction、Precision、NumericScale、Size与Type属性可以指定参数的信息,通过执行AppendChunk方法可以将数据传递到参数里。

    • Recordset对象

    如果执行的命令是一个查询并返回存放在表中的结果集,这些结果集将被保存在本地的存储区里,Recordset对象是执行这种存储的ADO对象。通过Recordset对象可以操纵来自数据提供者的数据,包括修改和更新行、插入和删除行。

    ADO定义了如表

    光标类型

    描述

    adOpenDynamic

    允许添加、修改和删除记录,支持所有方式的光标移动,其他用户的修改可以在联机以后仍然可见

    adOpenKeyset

    类似于adOpenDynamic光标,它支持所有类型的光标移动,但是建立连接以后其他用户对记录的添加不可见,其他用户对记录的删除和对数据的修改是可见的。支持书签操作

    adOpenStatic

    支持各种方式的光标移动,但是建立连接以后其他用户的行添加、行删除和数据修改都不可见,支持书签操作

    adOpenForwardOnly

    只允许向前存取,而且在建立连接以后,其他用户的行添加、行删除和数据修改都不可见,支持书签操作

    ADO定义了如表

    锁定类型

    描述

    adLockReadOnly

    (缺省)数据只读

    adLockPessimistic

    锁定操作的所有行,也称为消极锁定

    adLockOptimistic

    只在调用Update方法时锁定操作的行,也称为积极锁定

    adLockBatchOptimistic

    在批量更新时使用该锁定,也称为积极批量锁定

    ADO定义了如表

    光标服务位置

    描述

    adUseNone

    不使用光标服务位置

    adUseClient

    使用客户端光标

    adUseServer

    (缺省)使用数据服务端或者驱动提供端光标

    借助于Recordset对象的集合、方法和属性,可以通过设置CursorType属性设置记录集的光标类型,通过设置CursorLocation属性可以指定光标位置,通过读取BOF和EOF属性的值,获知当前光标在记录集里的位置是在最前或者最后,通过执行MoveFirst、MoveLast、MoveNext和MovePrevious方法移动记录集里的光标,通过执行Update方法可以更新数据修改,通过执行AddNew方法可以执行行插入操作,通过执行Delete方法可以删除行。

    • Field对象

    Recordset对象的一个行由一个或者多个Fields对象组成,如果把一个Recordset对象看成一个二维网格表,那么Fields对象就是这些列。这些列里保存了列的名称、数据类型和值,这些值是来自数据源的真正数据。为了修改数据源里的数据,必须首先修改Recordset对象各个行里Field对象里的值,最后Recordset对象将这些修改提交到数据源。

    借助于Field对象的集合、方法和属性,可以通过读取Name属性,获知列的名称。通过操作Value属性可以改变列的值,通过读取Type、Precision和NumericScale属性,可获知列的数据类型、精度和小数位的个数,通过执行AppendChunk和GetChunk方法可以操作列的值。

    • Error对象

    Error对象包含了ADO数据操作时发生错误的详细描述,ADO的任何对象都可以产生一个或者多个数据提供者错误,当错误发生时,这些错误对象被添加到Connection对象的Errors集合里。当另外一个ADO对象产生一个错误时,Errors集合里的Error对象被清除,新的Error对象将被添加到Errors集合里。

    借助于Errosr对象的集合、方法和属性,可以通过读取Number和Description属性,获得ADO错误号码和对错误的描述,通过读取Source属性得知错误发生的源。

    • Property对象

    Property对象代表了一个由提供者定义的ADO对象的动态特征。ADO对象有两种类型的Property对象:内置的和动态的。内置的Property对象是指那些在ADO里实现的在对象创建时立即可见的属性,可以通过域作用符直接操作这些属性。动态的Property对象是指由数据提供者定义的底层的属性,这些属性出现在ADO对象的Properties集合里,例如,如果一个Recordset对象支持事务和更新,这些属性将作为Property对象出现在Recordset对象的Properties集合里。动态属性必须通过集合进行引用,比如使用下面的语法:

           MyObject.Properties(0)   

    或者

           MyObject.Properties("Name")

    不能删除任何类型的属性对象。借助于Property对象的集合、方法和属性,可以通过读取Name属性获得属性的名称,通过读取Type属性获取属性的数据类型,通过读取Value属性获取属性的值。

    三、ADO编程

    通常情况下,一个基于ADO的数据库应用使用如下过程操作数据源里的数据:

    (1)创建一个Connection对象。定义用于连接的字符串信息,包括数据源名称、用户ID、口令、连接超时、缺省数据库以及光标的位置。一个Connection对象代表了同数据源的一次会话。可以通过Connection对象控制事务,即执行BeginTrans、CommitTrans和RollbackTrans方法。

    (2)打开数据源,建立同数据源的连接。

    (3)执行一个SQL命令。一旦连接成功,就可以运行查询了。可以以异步方式运行查询,也可以异步地处理查询结果,ADO会通知提供者后台提供数据。这样可以让应用程序继续处理其它事情而不必等待。

    (4)使用结果集。完成了查询以后,结果集就可以被应用程序使用了。在不同的光标类型下,可以在客户端或者服务器端浏览和修改行数据。

    (5)终止连接。当完成了所有数据操作后,可以销毁这个同数据源的连接。

    首先说句题外话,关于ADO各个对象的使用,可查看《ADL帮助文档》,如下是文档下载链接:http://download.csdn.net/detail/mingojiang/5017094

    3.1准备工作

    引入ADO库

            ADO是以DLL封装的,要使用ADO,首先得引入其DLL库,引入ADO类型库的方法有多种,在此我们就介绍一种----通过预处理指令#import引入。一般情况下,在windows操作系统的Program FilesCommon FilesSystemado目录下都有一个msado*.dll文件,根据windows版本不同,该文件可以是msado1.dll、msado15.dll、msado2.dll,对于此文件,版本不一样,可能导致一些冲突,如:系统自带的msado*.dll可能与程序编译所用的版本不一样,可能会导致一些异常,那么对于这种情况,我个人一般的做法是,在软件目录下面带上msado*.dll文件,这样就排除了对软件运行环境的依赖。

    Ado类型库引入后,程序在编译过程中,VC++会读出msado*.dll中的类型库信息,自动产生两个该类型库的头文件和实现文件msado15.tlh和msado15.tli(在您的Debug或Release目录下)。在这两个文件里定义了ADO的所有对象和方法,以及一些枚举型的常量等。以下是引入方法:

    //收下引入命令会导致4146警告,通过些命令去除

    #pragma warning(disable:4146)

    //引号里面的路径,可绝对的,可相对的

    #import"..CommFileLibmsado15.dll" named_guids

    //重命名EOF与BOF以免与其他命名空间冲突

    rename("EOF","adoEOF"),rename("BOF","adoBOF")

    #pragma warning(default:4146)

    using namespace ADODB;

    初始化COM

            ADO是COM组件,使用任何COM组件的时候,都要先初始化COM,其实就是在使用COM组件前调用一个API----CoInitialize(),在使用ADO对象之前调用即可,以下为代码:

    HRESULT hr =CoInitialize(NULL);

    ASSERT(SUCCEEDED(hr));//返回值可判断初始化COM是否成功,请用SUCCEEDED来判断

    一般情况下,使用完COM之后,调用反初始化COM函数----CoUninitialize()。一般会在数据库访问封装类的析构函数中调用。

    3.2连接数据库

            通过创建连接对象连接数据库,首先创建一个指向Connection的指针_ConnectionPtr,以下是创建连接的源码:

    _ConnectionPtr m_pConn;

    HRESULT hr =CoInitialize(NULL);

    ASSERT(SUCCEEDED(hr));

    if (NULL ==m_pConn) {

            try

            {  //Connecting

                      if ( !FAILED(m_pConn.CreateInstance(_uuidof(Connection)))){ //设置连接超时时间

                               m_pConn->CommandTimeout= 30;                  //设置连接超时值,单位为秒

                               if (!FAILED(m_pConn->Open((_bstr_t)(m_szConnStr), "", "",adModeUnknown)))

                                        returntrue;

                      }

            }

            catch(_com_error e){

                      char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                      sprintf(szLog, "连接数据库错误:%s ",(char *)(e.Description()));

            }

            return false;

    }

    Open方法中,第一个参数就是连接串,这个连接串如何获得?VC++6.0中有工具,不过太过麻烦,而且获得的串还得复制,不方便,下面介绍一种方便实用获得连接串的方法。

    获得连接串

            创建一个记事本文件,把文件名(包括扩展名)改成“Test.udl”,双击打开,如下图:

    第一个红色框里有三个记录,针对ACCESS数据库可选前三项中的一项。Oracle数据库选第二个框,第三个框是对MS Sql数据库的。自己所连的数据库是什么类型,自己选择,然后点下一步,如图:

    第一个框输入数据库所在服务器的IP,或者计算机名,或者域名之类,第二个框输入登录数据库的用户名与密码,勾上第三个框中的复选框,这时,如果你上面几项的资料都没错的话,第四个框中会出现你所连接的数据库中的所有的库名,然后你选一个你所要连接的库,点击第五个框中的“测试连接”按钮,如果弹出连接成功之类的提示,那么恭喜,成功了,点“确定”按钮关闭窗口,再以记事本方式打开此文件,你就会看到你所需的连接串了,效果图如下:

    红色框中的串就是连接串,把这一串数据复制下来,赋给Open方法中的第一个参数,就OK了,但是这是不是还是有点麻烦呀,要复制来复制去的,这还有更便捷的方法,以上方法不变,只是使用连接串的方法做些改进,只要把“Test.udl”文件的路径赋给Open方法的第一个参数即可,如下:

    char szUdlPath[] ={".\Test.udl"};//可相对路径,亦可绝对路径

    sprintf(m_szConnStr,"File Name=%s", szUdlPath);//前面是固定格式

    按这种方法,发行软件的时候,只要相应目录下,放一个“Test.udl”文件即可,数据库的连接可自由配制,是不是相当便捷实用呀。

    3.3获得记录集

            与Connection对象一样,首先是创建一个RecordSet对象指针,再创建对象

    try

    {

            if(!FAILED(m_pRecord.CreateInstance(__uuidof(Recordset)))) {

                      HRESULT hr = 0;

                      //当连接断开,或者连接对象没有连接的情况下,通过连接串获得记录集

                      if (NULL == m_pConn ||adStateClosed == m_pConn->State)

                               hr =m_pRecord->Open((_bstr_t)szSQL, _variant_t(m_szConnStr),

    adOpenKeyset, adLockOptimistic,adCmdText);

                      else//连接对象处理正常连接状态下,直接使用

                               hr = m_pRecord->Open((_bstr_t)szSQL,m_pConn.GetInterfacePtr(), adOpenKeyset,

                                                                              adLockOptimistic,adCmdText);

                      if (SUCCEEDED(hr)) 

                               bResult = true;

            }

    }

    catch (_com_errore)

    {

            char szLog[MAX_BUFF]; memset(szLog, 0,MAX_BUFF);

            sprintf(szLog, "执行SQL查询命令错误:%s[%s]",(char *)(e.Description()), szSQL);

    }

    以上代码中的szSQL是select串,就是查询语句,if与else中m_pRecord有两种Open方式,第一种是连接对象断开,或者没连接状态下,通过连接串获得记录集,第二种连接正常情况下,那么无需再连接,直接通过连接对象获得连接信息。至此,记住集已成功获得,下面是怎么提取数据的问题了。

    while(!IsRecordEOF()){//判断是否到了记录结尾

            UINT nCarID = 0;

            char sCarMark[MAX_BUFF];memset(sCarMark, 0, MAX_BUFF);

            CollectMsg("CARID"                  ,                nCarID                                  )     ;//第一个参数,传列名

            CollectMsg("Car_Mark"           ,                sCarMark,         MAX_BUFF)      ;

            RecordMoveNext();//移动到下一个记录

    }

    CloseRecord ();//关闭记录集

     

    以下代码是以上代码的函数实现:

    Bool IsRecordEOF()

    {

            bool bResult = true;

            if (NULL != m_pRecord) {

                      if (!m_pRecord->adoEOF)

                               bResult = false;

            }

            return bResult;

    }

     

    Bool CollectMsg(const char *szColumnName, int&iTemp            )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_t Column(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               iTemp =RusultGet.intVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    Bool CollectMsg(const char *szColumnName, char*szBuff, int nBuffSize)

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_t Column(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               WideCharToMultiByte(CP_ACP,0, RusultGet.bstrVal, -1, szBuff, nBuffSize,

    NULL, NULL);

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

    Bool RecordMoveNext()

    {

            bool bResult = false;

            if (NULL != m_pRecord) {

                      if (!m_pRecord->adoEOF) {

                               try{

                                        m_pRecord->MoveNext();

                                        bResult =true;

                               }

                               catch(_com_error e){

                                        charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                                        sprintf(szLog,"RecordMoveNext错误:%s ", (char *)(e.Description()));

                                        m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                               }

                      }

            }

            return bResult;

    }

     

    Void CloseRecord  (void){

            try{

                      if (NULL != m_pRecord){

                               m_pRecord->Close();

                               m_pRecord = NULL;

                      }

            }

            catch(_com_error e){

                      char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                      sprintf(szLog, "关闭数据记录集错误:%s ",(char *)(e.Description()));

                      m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

            }

    }       

    3.4执行SQL命令

            可通过Command对象来执行命令,也可以直接通过Connection对象来执行,以下介绍通过Connection对象来执行命令,命令可以是Updata命令,Insert命令等,以下是代码:

    //第一个参数是要执行的命令,第二个参数用来返回影响的行数

    Bool ExecuteSQL(const char *szSQLStr, long&nRefreshNum){

            bool bResult = false;

            if (strlen(szSQLStr) > 0) {

                      if (NULL == m_pConn ||adStateClosed == m_pConn->State)

                               ConnDB();

                      try

                      {

                               _variant_tRefreshNum;

                               m_pConn->Execute(_bstr_t(szSQLStr),&RefreshNum, adCmdText);

                               bResult = true;

                               nRefreshNum =RefreshNum.lVal;

                      }

                      catch (_com_error e)

                      {

                               nRefreshNum = 0;

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"执行SQL命令错误:%s[%s]", (char *)(e.Description()), szSQLStr);

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

    3.5关闭连接、记录集

            连接对象与记录集使用完之后,要关闭:        

    if (NULL !=m_pConn)

            m_pConn->Close();

    if (NULL !=m_pRecord){

            m_pRecord->Close();

     

    三、示例代码

            本人对数据库访问、操作初步封装了一个简单的类,进行了初步错误处理,下载链接:

    http://download.csdn.net/detail/mingojiang/5019767

    以下是部份代码,头文件部份:

    /*****************************************************************************

     * 实现功能        :ADO数据库封装类

     * 作者                  :Mingo(QQ1148824289)

     * 版本                  :V 1.0.1

     * 完成日期        :2011.12.15

     * 其它说明        :

     * 修改记录1     :宽窄字符环境兼容性处理

     *   日 期        :2011.12.30

     *   修改人        :Mingo             

     * 修改记录2:

     *****************************************************************************/

     

    #if !defined(AFX_MYDBWORK_H__D777F369_68E5_4FA3_8198_6048A5DE7708__INCLUDED_)

    #defineAFX_MYDBWORK_H__D777F369_68E5_4FA3_8198_6048A5DE7708__INCLUDED_

     

    #if _MSC_VER >1000

    #pragma once

    #endif // _MSC_VER> 1000

     

     

    #pragmawarning(disable:4146)

    #import "..CommFileLibmsado15.dll"named_guids rename("EOF","adoEOF"),rename("BOF","adoBOF")

    #pragmawarning(default:4146)

    using namespaceADODB;

     

    #include"..\CommFile\CMyWriteLog.h"

    //#include"CMyWriteLog.h"

    #include<icrsint.h>

    _COM_SMARTPTR_TYPEDEF(IADORecordBinding,__uuidof(IADORecordBinding));

    #define MAX_BUFF260

     

     

    classCMyDBWork 

    {

    public:

            CMyDBWork();

            virtual ~CMyDBWork();

     

     

    public:

            bool BindingRecord(CADORecordBinding* RsObject, char *szSQLStr);

           bool ConnDB             (void);                                                                       //连接数据库

           int              GetConnStr      (char *szConnStr, int nBuffSize);      //获得连接数据库的字符串

            bool SetConnStr       (void);                                                                       //设置连接数据库的字符串      

            bool SetConnStr       (char *szConnStr);                                        //设置连接数据库的字符串      

           bool SelectSQL          (constchar *szSQL);                                    //执行select语句

           bool ExecuteSQL      (constchar *szSQLStr, long &nRefreshNum);

            void  CloseDB             (void);                                                                       //关闭记录集、连接对象     

            void CloseRecord      (void);      

    public:

            bool BeginTrans();                                //启动一新事务,相当于建立回滚标志

            bool CommitTrans();                                     //提交BeginTrans之后所有的修改

            bool RollbackTrans();                           //取消BeginTrans之后所有的操作

            bool RecordMoveLast();

            bool RecordMoveFirst();

            bool RecordMovePrevious();

            bool RecordMoveNext();

            bool IsRecordBOF();

            bool IsRecordEOF();

     

            

     

            bool CollectMsg       (const char *szColumnName, long &lTemp             );

            bool CollectMsg       (const char *szColumnName, BYTE &btTemp                  );

            bool CollectMsg       (const char *szColumnName, DWORD&dwTemp );

            bool CollectMsg       (const char *szColumnName, UINT&iTemp            );

            bool CollectMsg       (const char *szColumnName, int &iTemp                );

            bool CollectMsg       (const char *szColumnName, float&fTemp            );

            bool CollectMsg       (const char *szColumnName, double&dbTemp     );

            bool CollectMsg       (const char *szColumnName, char&chTemp          );

            bool CollectMsg       (const char *szColumnName, char *szBuff,int nBuffSize);

     

            bool CollectMsg       (ULONG nIndex, long      &lTemp             );

            bool CollectMsg       (ULONG nIndex, BYTE &btTemp              );

            bool CollectMsg       (ULONG nIndex, DWORD &dwTemp      );

            bool CollectMsg       (ULONG nIndex, UINT &iTemp                 );

            bool CollectMsg       (ULONG nIndex, int &iTemp            );

            bool CollectMsg       (ULONG nIndex, float &fTemp                  );

            bool CollectMsg       (ULONG nIndex, double &dbTemp );

            bool CollectMsg       (ULONG nIndex, char &chTemp               );

            bool CollectMsg       (ULONG nIndex, char *szBuff, intnBuffSize);

     

    //private:

            CMyWriteLog m_WriteLog;

    //private:

            char m_szConnStr[MAX_BUFF];

    //private:

            _ConnectionPtr m_pConn;

            _RecordsetPtr  m_pRecord;

            _CommandPtr    m_pCommand;

            _ParameterPtr  m_pParameter;

    };

     

    #endif //!defined(AFX_MYDBWORK_H__D777F369_68E5_4FA3_8198_6048A5DE7708__INCLUDED_)

     

     

     

    //////////////////////////////////////////////////////////////////////////

    ////////////////////以下是以绑定记录集方式提取数据的Demo//////////////////

    //////////////////////////////////////////////////////////////////////////

    // CMyDBWorkDbObject;

    //if(DbObject.ConnDB())

    // {

    //    CCarInfoObj CarObj;

    //    if (DbObject.BindingRecord(&CarObj,"SELECT top(10)Car_mark, carid,

    gsm_number, car_type FROMcar_info")) {

    //             while (! DbObject.IsRecordEOF()) {

    //                      if (adFldOK ==CarObj.m_nStatusArray[0] && adFldOK ==

                                        CarObj.m_nStatusArray[0]&& adFldOK== CarObj.m_nStatusArray[0]) {

    //                                CString szShow;

    //                                szShow.Format("CarMark:%s,CarID:%d, GSM:%s", CarObj.m_szCarMark,

                                                                    CarObj.m_nCarID,CarObj.m_szGSM);

    //                                AfxMessageBox(szShow);

    //                      }

    //                      DbObject.RecordMoveNext();

    //             }

    //    }

    // }

    // else{

    //    char szBuff[100]; memset(szBuff, 0, 100);

    //    int nR = GetErrorStr(szBuff,GetLastError());

    //    int i = 0;

    // }

     

    //////////////////////////////////////////////////////////////////////////

    /////////////////////////以下是关于CarObj的定义///////////////////////////

    //////////////////////////////////////////////////////////////////////////

    // #include"..COMMFILEMyDBWork.h"

    // #include"icrsint.h"

    //

    // classCCarInfoObj : public CADORecordBinding 

    // {

    //    BEGIN_ADO_BINDING(CCarInfoObj)

    //             ADO_VARIABLE_LENGTH_ENTRY2(1,adVarChar, m_szCarMark, sizeof(m_szCarMark)       ,m_nStatusArray[0], false)

    //             ADO_VARIABLE_LENGTH_ENTRY2(2,adInteger, m_nCarID, sizeof(m_nCarID)            , m_nStatusArray[1], false)

    //             ADO_VARIABLE_LENGTH_ENTRY2(3,adVarChar, m_szGSM, sizeof(m_szGSM)            , m_nStatusArray[2], false)

    //             END_ADO_BINDING()

    //            

    // public:

    //    char m_szCarMark[20];

    //    ULONG m_nCarID;

    //    char m_szGSM[20];

    //    ULONG m_nStatusArray[3];

    //   

    // public:

    //    CCarInfoObj();

    //    virtual ~CCarInfoObj();

    //   

    // };

     

    以下是部份代码,实现部份:

    // MyDBWork.cpp:implementation of the CMyDBWork class.

    //

    //////////////////////////////////////////////////////////////////////

     

    #include"stdafx.h"

    #include"MyDBWork.h"

    //#include<icrsint.h>

     

    #ifdef _DEBUG

    #undef THIS_FILE

    static charTHIS_FILE[]=__FILE__;

    #define newDEBUG_NEW

    #endif

     

    //////////////////////////////////////////////////////////////////////

    //Construction/Destruction

    //////////////////////////////////////////////////////////////////////

     

    CMyDBWork::CMyDBWork()

    {

            m_pConn = NULL;

            m_pRecord = NULL;

            

            char szPath[MAX_BUFF]; memset(szPath,0, MAX_BUFF);

            m_WriteLog.GetLogPath(szPath,MAX_BUFF);

            if (strlen(szPath) > 0) {

                      strcat(szPath,"DBError\");

                      m_WriteLog.SetLogPath(szPath);

            }

            

            SetConnStr();

            HRESULT hr = CoInitialize(NULL);

            if (FAILED(hr)){

                      m_WriteLog.WriteLog("CoInitializefail ", strlen("CoInitialize fail "), LOG_ERR);

            }

    }

     

    CMyDBWork::~CMyDBWork()

    {

            CoUninitialize();

    }

     

    boolCMyDBWork::CollectMsg(const char *szColumnName, long &lTemp             )

    {

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               lTemp =RusultGet.lVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

    boolCMyDBWork::CollectMsg(const char *szColumnName, BYTE &btTemp                  )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               btTemp =RusultGet.bVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

    boolCMyDBWork::CollectMsg(const char *szColumnName, DWORD &dwTemp)

    {

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               dwTemp =RusultGet.ulVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

    boolCMyDBWork::CollectMsg(const char *szColumnName, UINT &iTemp           )

    {

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               iTemp =RusultGet.ulVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

    boolCMyDBWork::CollectMsg(const char *szColumnName, int &iTemp                )

    {

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               iTemp =RusultGet.intVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    boolCMyDBWork::CollectMsg(const char *szColumnName, float &fTemp            )

    {

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               fTemp =RusultGet.fltVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

    boolCMyDBWork::CollectMsg(const char *szColumnName, double &dbTemp     )

    {

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               dbTemp =RusultGet.dblVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::CollectMsg(const char *szColumnName, char &chTemp){

            bool bResult = false;

            if (adStateClosed != m_pRecord->State){

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               chTemp =char(RusultGet.bstrVal[0]);

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::CollectMsg(const char *szColumnName, char *szBuff, int nBuffSize)

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn(szColumnName);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               WideCharToMultiByte(CP_ACP,0, RusultGet.bstrVal, -1, szBuff, nBuffSize,

    NULL, NULL);

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::CollectMsg(ULONG nIndex, long      &lTemp             )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               lTemp =RusultGet.lVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

     

            return bResult;

    }

    boolCMyDBWork::CollectMsg(ULONG nIndex, BYTE &btTemp              )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               btTemp =RusultGet.bVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

    boolCMyDBWork::CollectMsg(ULONG nIndex, DWORD &dwTemp      )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               dwTemp =RusultGet.ulVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

    boolCMyDBWork::CollectMsg(ULONG nIndex, UINT &iTemp                 )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               iTemp =RusultGet.ulVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

    boolCMyDBWork::CollectMsg(ULONG nIndex, int &iTemp            )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               iTemp =RusultGet.intVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    boolCMyDBWork::CollectMsg(ULONG nIndex, float &fTemp                 )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               fTemp =RusultGet.fltVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

    boolCMyDBWork::CollectMsg(ULONG nIndex, double &dbTemp )

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               dbTemp =RusultGet.dblVal;

                               bResult = true;

                      }

                      catch(_com_error e){

                               char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    // boolCMyDBWork::CollectMsg(ULONG nIndex, char &chTemp){

    //    bool bResult = false;

    //    if (adStateClosed != m_pRecord->State) {

    //             try{

    //                      _variant_tColumn((long)nIndex);

    //                      _variant_t RusultGet =m_pRecord->Fields->GetItem(Column)->Value;

    //                      chTemp =char(RusultGet.cVal);

    //                      bResult = true;

    //             }

    //             catch(_com_error e){

    //                      char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

    //                      sprintf(szLog,"%s ", CW2A(e.Description()));

    //                      m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

    //             }

    //    }

    //    return bResult;

    // }

     

     

    boolCMyDBWork::CollectMsg(ULONG nIndex, char *szBuff, int nBuffSize)

    {

            bool bResult = false;

            if (adStateClosed !=m_pRecord->State) {

                      try{

                               _variant_tColumn((long)nIndex);

                               _variant_t RusultGet= m_pRecord->Fields->GetItem(Column)->Value;

                               WideCharToMultiByte(CP_ACP,0, RusultGet.bstrVal, -1, szBuff, nBuffSize,

     NULL, NULL);

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::ConnDB(void){

            bool bResult = false;

            if (NULL == m_pConn || adStateClosed ==m_pConn->State) {

                      if (strlen(m_szConnStr) >0) {

    //                      HRESULT hr =CoInitialize(NULL);

    //                      if (FAILED(hr))         {

    //                                m_WriteLog.WriteLog("CoInitializefail ", strlen("CoInitialize fail "), LOG_ERR);

    //                      }

    //                        else{

                               

                                        try

                                        {   //Connecting

                                                  if( ! FAILED(m_pConn.CreateInstance(_uuidof(Connection)))){

    //设置连接超时时间

                                                           m_pConn->CommandTimeout= 30;                  //in seconds

                                                           if(! FAILED(m_pConn->Open((_bstr_t)(m_szConnStr), "", "",

                                                                                       adModeUnknown)))

                                                                    bResult= true;

                                                  }

                                        }

                                        catch(_com_errore){

                                                  charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                                                  sprintf(szLog,"连接数据库错误:%s ", (char *)(e.Description()));

                                                  m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                                        }

                      //      }

                      }

            }

            

            return bResult;

    }

     

     

    boolCMyDBWork::SelectSQL(const char *szSQL){

            bool bResult = false;

            if (strlen(szSQL) > 0 &&strlen(m_szConnStr) > 0) {

                      if (NULL != m_pRecord/* ||adStateClosed != m_pRecord->State*/)

                               m_pRecord->Close();

                      

                      try

                      {

                               if(!FAILED(m_pRecord.CreateInstance(__uuidof(Recordset)))) {

                                        HRESULT hr= 0;

                                        if (NULL ==m_pConn || adStateClosed == m_pConn->State)

                                                  hr= m_pRecord->Open((_bstr_t)szSQL, _variant_t(m_szConnStr),

                                                                                       adOpenKeyset,adLockOptimistic, adCmdText);

                                        else

                                                  hr= m_pRecord->Open((_bstr_t)szSQL, m_pConn.GetInterfacePtr(),

                                                                              adOpenKeyset,adLockOptimistic, adCmdText);

                                        if(SUCCEEDED(hr)) 

                                                  bResult= true;

                               }

                      }

                      catch (_com_error e)

                      {

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"执行SQL查询命令错误:%s[%s]", (char*)(e.Description()), szSQL);

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    //nRefreshNum受影响的记录数

    boolCMyDBWork::ExecuteSQL(const char *szSQLStr, long &nRefreshNum){

            bool bResult = false;

            if (strlen(szSQLStr) > 0) {

                      if (NULL == m_pConn ||adStateClosed == m_pConn->State)

                               ConnDB();

                      try

                      {

                               _variant_t RefreshNum;

                               m_pConn->Execute(_bstr_t(szSQLStr),&RefreshNum, adCmdText);

                               bResult = true;

                               nRefreshNum =RefreshNum.lVal;

                      }

                      catch (_com_error e)

                      {

                               nRefreshNum = 0;

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"执行SQL命令错误:%s[%s]", (char *)(e.Description()), szSQLStr);

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    intCMyDBWork::GetConnStr(char *szConnStr, int nBuffSize){

            int nResult = 0;

            if (NULL != szConnStr &&nBuffSize > 1 && strlen(m_szConnStr) > 0){

                      if (strlen(m_szConnStr) <=(unsigned int)nBuffSize)        {

                               strcpy(szConnStr,m_szConnStr);

                               nResult =strlen(m_szConnStr);

                      }

            }

     

            return nResult;

    }

     

    boolCMyDBWork::SetConnStr        (void){                          //设置连接数据库的字符串

            bool bResult = true;

            try{

                      GetPrivateProfileStringA("DATA_BASE",   "ConnectStr", "",m_szConnStr,

    MAX_BUFF, ".\Config.ini");

                      if (strlen(m_szConnStr) <1) {

                               char szUdlPath[] ={".\Test.udl"};

                               WIN32_FIND_DATAAfindData;

                               if(INVALID_HANDLE_VALUE == FindFirstFileA(szUdlPath, &findData)){

                                        FILE *hFile= fopen(szUdlPath, "a");

                                        if (NULL !=hFile) {

                                                  fclose(hFile);

                                        }

                               }

                               sprintf(m_szConnStr,"File Name=%s", szUdlPath);

                      }

            }

            catch(...){

                      bResult = false;

            }

            

            return bResult;

    }

    bool CMyDBWork::SetConnStr        (char *szConnStr){

            bool bResult = false;

            if (NULL != szConnStr &&strlen(szConnStr) > 0) {

                      strcpy(m_szConnStr,szConnStr);

                      bResult = true;

            }

            return bResult;

    }

     

    voidCMyDBWork::CloseDB(void){

            CloseRecord();

            try{

                      if (NULL != m_pConn)

                               m_pConn->Close();

            }

            catch(_com_error e){

                      char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                      sprintf(szLog, "关闭数据库连接错误:%s ",(char *)(e.Description()));

                      m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

            }

    }

     

    void CMyDBWork::CloseRecord      (void){

            try{

                      if (NULL != m_pRecord){

                               m_pRecord->Close();

                               m_pRecord = NULL;

                      }

            }

            catch(_com_error e){

                      char szLog[MAX_BUFF];memset(szLog, 0, MAX_BUFF);

                      sprintf(szLog, "关闭数据记录集错误:%s ",(char *)(e.Description()));

                      m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

            }

    }       

     

    boolCMyDBWork::BindingRecord(CADORecordBinding *RsObject, char *szSQLStr)

    {

            bool bResult = false;

            

            CloseRecord();

            if (SelectSQL(szSQLStr)) {

                      try{

                               IADORecordBindingPtrpicRs(m_pRecord);

                               if (! FAILED(picRs->BindToRecordset(RsObject)))

                                        bResult =true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"绑定记录集错误:%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    boolCMyDBWork::IsRecordEOF()

    {

            bool bResult = true;

            if (NULL != m_pRecord) {

                      if (!m_pRecord->adoEOF)

                               bResult = false;

            }

            return bResult;

    }

     

    boolCMyDBWork::IsRecordBOF()

    {

            bool bResult = true;

            if (NULL != m_pRecord) {

                      if (!m_pRecord->adoBOF)

                               bResult = false;

            }

            return bResult;

    }

     

    boolCMyDBWork::RecordMoveNext()

    {

            bool bResult = false;

            if (NULL != m_pRecord) {

                      if (!m_pRecord->adoEOF) {

                               try{

                                        m_pRecord->MoveNext();

                                        bResult =true;

                               }

                               catch(_com_errore){

                                        charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                                        sprintf(szLog,"RecordMoveNext错误:%s ", (char *)(e.Description()));

                                        m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                               }

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::RecordMovePrevious()

    {

            bool bResult = false;

            if (NULL != m_pRecord) {

                      if (!m_pRecord->adoBOF) {

                               try{

                                        m_pRecord->MovePrevious();

                                        bResult =true;

                               }

                               catch(_com_error e){

                                        charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                                        sprintf(szLog,"RecordMovePrevious错误:%s ", (char *)(e.Description()));

                                        m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                               }

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::RecordMoveFirst()

    {

            bool bResult = false;

            if (NULL != m_pRecord) {

                      try{

                               m_pRecord->MoveFirst();

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"RecordMoveFirst错误:%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            return bResult;

    }

     

    boolCMyDBWork::RecordMoveLast()

    {

            bool bResult = false;

            if (NULL != m_pRecord) {

                      try{

                               m_pRecord->MoveLast();

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog, "RecordMoveLast错误:%s ",(char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    boolCMyDBWork::BeginTrans()

    {

            bool bResult = false;

            if (NULL != m_pConn) {

                      try{

                               m_pConn->BeginTrans();

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"BeginTrans错误:%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    bool CMyDBWork::CommitTrans()

    {

            bool bResult = false;

            if (NULL != m_pConn) {

                      try{

                               m_pConn->CommitTrans();

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"CommitTrans错误:%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    boolCMyDBWork::RollbackTrans()

    {

            bool bResult = false;

            if (NULL != m_pConn) {

                      try{

                               m_pConn->RollbackTrans();

                               bResult = true;

                      }

                      catch(_com_error e){

                               charszLog[MAX_BUFF]; memset(szLog, 0, MAX_BUFF);

                               sprintf(szLog,"RollbackTranst错误:%s ", (char *)(e.Description()));

                               m_WriteLog.WriteLog(szLog,strlen(szLog), LOG_ERR);

                      }

            }

            

            return bResult;

    }

     

    while(!IsRecordEOF()){

            UINT nCarID = 0;

            char sCarMark[MAX_BUFF];memset(sCarMark, 0, MAX_BUFF);

            CollectMsg("CARID"                           ,                nCarID                                           )       ;       

            CollectMsg("Car_Mark"           ,                sCarMark,                  MAX_BUFF)     ;

            v_CarInfo.push_back(CI);

            RecordMoveNext();

    }

    CloseDB();

  • 相关阅读:
    sublime打开txt文件乱码的问题
    while循环小例
    mongoDB内置文档定义
    WebStorm 10.0.3注册码
    angularjs之ng-mode获取lobject类型里的键值
    前端打印console
    js去掉数组的空字符串
    js数组去重的三种方式的比较
    js数据类型之判断
    Bootstrap中的datetimepicker浅谈
  • 原文地址:https://www.cnblogs.com/MingoJiang/p/8682201.html
Copyright © 2020-2023  润新知