一个用功能强大的ODBC API 函数访问数据库类

发表于:2007-07-14来源:作者:点击数: 标签:
/*-- 专项技术学习 - 数据库 访问技术之ODBC API 这个类 通过ODBC API 函数访问ODBC 数据源的数据表 功能简介:能够帮定一个临时指定表,调用类的函数即可对该表 插入、增加、修改、简单统计等等操作(注:通过动态改变表的信息:表名字、 表字段总数、字段名字
/*-- 专项技术学习 -数据库访问技术之ODBC API                 
     这个类 通过ODBC API 函数访问ODBC 数据源的数据表            
     功能简介:能够帮定一个临时指定表,调用类的函数即可对该表    
     插入、增加、修改、简单统计等等操作(注:通过动态改变表的信息:表名字、
  表字段总数、字段名字、字段类型、字段是否为数值、字段是否为主键。这样
  就可以访问ODBC 数据源的所有表了)                          
     另外提供一个函数 可以对该数据源的其他表(包括帮定的表)进行 
     删除、修改功能                                               
     LSZ 2001.08.25                                                  */

/*头文件*/

#ifndef _WDC_DATABASE_H
#define _WDC_DATABASE_H

#include <sql.h>
#include <sqlext.h>

#define  MAX_FIELDS     500
#define  MAX_ERROR_MSG  1024
#define  MAX_DATA     500

class CWdcDatabase  
{
public:
CWdcDatabase();
virtual ~CWdcDatabase();
    BOOL Open(const CString& sDSN,
     const CString& sTable,
     const CString& sUserId,
     const CString& sPassword)  ; /*打开一个ODBC数据源数据库*/

public:
BOOL m_bOpen;  /*标志数据库是否打开*/
BOOL m_bBrackets; /*标识表名、字段名是否用方扩号扩起来*/
int m_nError;  /*ODBC错误返回代码*/
CString m_sQuery; /*操作ODBC 数据源公用的语句,用于方便构造*/
CString m_sErrMsg; /*ODBC错误返回信息*/
CString m_sDSN;  /*ODBC 数据源的 DSN*/
CString m_sTable;   /*临时绑定的表名*/

public:
void DropHstmtSelect(); /*确保公共查询的语句句柄释放*/
long GetRecordCount(CString &sSel); /*取得符合条件的记录数*/
BOOL NextMatchingRecord(); /*查询符合条件的下一条记录*/
int SelectMatchingRecords(CString& sWhereContext); /*查询符合条件的记录*/
BOOL UpdateRecord(); /*更新临时绑定的表的一条记录*/
BOOL AddRecord();    /*增加临时绑定表一条新记录*/
BOOL DeleteRecord(CString &sWhereDelete); /*删除临时绑定表符合条件记录*/
BOOL Execute(const CString  &sText); /*执行一个SQL语句,注:可以执行存储过程*/
void Close();        /*关闭数据库*/
public:
int m_nFields;       /*临时绑定表字段的总数*/
CString m_sFieldName[MAX_FIELDS]; /*临时绑定表字段名字*/
CString m_sFieldValue[MAX_FIELDS];/*临时绑定表字段值*/
BOOL m_bKeyField[MAX_FIELDS];/*临时绑定表字段是否为主键*/
BOOL m_bNumeric[MAX_FIELDS];/*临时绑定表字段是否为数值类型*/

private:
CString Quote(CString &sQuote); /*字符串替换*/
RETCODE rc;                     /*ODBC API 涵数执行返回码*/
HSTMT hstmt, hstmt_select; /*公共语句句柄*/
unsigned char szDSN[200]; /*连接数据源的 DSN */
unsigned char szID[200]; /*连接数据源的 UID */
unsigned char szPassword[200]; /*连接数据源的 PWD*/
unsigned char szQuery[3000]; /*公共执行的语句句柄*/
unsigned char szTemp[1000]; /*公共使用的字符串*/
CString sTemp ; /*公共使用的字符串*/
HENV henv;      /*环境句柄*/
HDBC hdbc;      /*ODBC 连接句柄*/
};

#endif


/*实现文件*/

#include "stdafx.h"
#include "WdcDatabase.h"
#include <sql.h>
#include <sqlext.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CWdcDatabase::CWdcDatabase()
{
  m_nError = 0;
m_bOpen = false;
m_nFields = 0;
m_bBrackets = false;
}

CWdcDatabase::~CWdcDatabase()
{
    if(m_bOpen)
{
  Close() ;
}
}

BOOL CWdcDatabase::Open(const CString& sDSN,
      const CString& sTable,
      const CString& sUserId,
      const CString& sPassword)
{
    if(m_bOpen)
{
  Close() ;
}

hstmt_select = NULL ;
RETCODE rc ;
CString stemp ;

memset(szDSN,0x00,200) ;
    memset(szID,0x00,200) ;
memset(szPassword,0x00,200) ;
/*Open Database */
SQLAllocEnv(&henv) ;
SQLAllocConnect(henv,&hdbc) ;

strcpy((char*)szDSN,sDSN) ;

stemp = sUserId ;
strcpy((char*)szID,stemp) ;

    stemp = sPassword ;
strcpy((char*)szPassword,stemp) ;

m_sDSN   = sDSN ;
m_sTable = sTable ;

rc = SQLConnect(hdbc,
     szDSN,
     SQL_NTS,
     szID,
     SQL_NTS,
     szPassword,
     SQL_NTS);

m_nError = (int) rc ;

if((rc == SQL_SUCCESS ) ||(rc == SQL_SUCCESS_WITH_INFO))
{
      m_bOpen = TRUE ;
   TRACE0("Open database Suclearcase/" target="_blank" >cceed !\n") ;
   return TRUE  ;
}
    else
{
      m_bOpen = FALSE ;
   TRACE0("Open Database Failed !\n") ;
   return FALSE ;
}

}

void CWdcDatabase::Close()
{
    if(m_bOpen)
{
  if(hstmt_select)
  {
   SQLFreeStmt(hstmt_select,SQL_DROP) ;
   hstmt_select = NULL ;
  }

  SQLDisconnect(hdbc) ;    /*Dis   hdbc*/
  SQLFreeConnect(hdbc) ;   /*Free  Odbc*/
  SQLFreeEnv(henv) ;       /*Free  henv*/  
  m_bOpen = false;
  TRACE0("Close database Succeed !\n") ;
}
}

BOOL CWdcDatabase::Execute(const CString &sSQL)
{
RETCODE rc ;
    unsigned char szSQLSTATE[6] ;
SDWORD nErr ;
unsigned char msg[MAX_ERROR_MSG + 1] ;
SWORD cbmsg ;

m_sErrMsg = "" ;
m_sQuery = sSQL ;

if(!m_bOpen)
  return FALSE ;

SQLAllocStmt(hdbc,&hstmt) ;
memset((char*)szQuery,0x00,sizeof(szQuery)) ;
strcpy((char*)szQuery,m_sQuery) ;

rc = SQLExecDirect(hstmt,
                 szQuery,
        SQL_NTS) ;

   m_nError = (int) rc ;
   if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
   {
       SQLTransact(henv,hdbc,SQL_COMMIT);
    SQLFreeStmt(hstmt,SQL_DROP) ;
    return TRUE ;
   }
   else
   {
     m_sErrMsg = "" ;
      while(SQLError(0,
               0,
      hstmt,
      szSQLSTATE,
      &nErr,
      msg,
      sizeof(msg),
      &cbmsg) == SQL_SUCCESS)
           m_sErrMsg = "\t\t" + (CString)msg + "\n" ;
      SQLFreeStmt(hstmt,SQL_DROP) ;
   return FALSE ;
   }

}

/*带入条件为 Where 以后的条件*/
BOOL CWdcDatabase::DeleteRecord(CString &sWhereDelete)
{
   RETCODE rc  ;
   unsigned char szSQLSTATE[6] ;
   SDWORD nErr ;
   unsigned char msg[MAX_ERROR_MSG+1] ;
   SWORD cbmsg ;

   CString sTemp ;

   if(!m_bOpen)
    return FALSE ;

   m_sQuery = "" ;
   m_sErrMsg = "" ;
   
   /*开始构造删除语句*/
   m_sQuery.Format("Delete From %s ",m_sTable) ;

/* int ff =0 ;
   for(int f= 0 ;f<m_nFields;f++)
   {
    if(m_bKeyField[f]) /*如果是表的主键
       {
         if(ff>0) /*如果主键超过2个,要把条件连接起来
    m_sQuery += " AND " ;
   ff++ ;

        if (m_bNumeric[f])
   sTemp.Format("%s=%s",m_sFieldName[f],m_sFieldValue[f]) ;
    else
   sTemp.Format("%s=%s",m_sFieldName[f],Quote(m_sFieldValue[f])) ;
         
    m_sQuery += sTemp ;
    }
   }
*/
    if(sWhereDelete !="")
       m_sQuery +=" Where " + sWhereDelete ;

    TRACE0(m_sQuery) ;
memset((char*)szQuery,0x00,sizeof(szQuery)) ;
    strcpy((char*)szQuery,m_sQuery) ;
    SQLAllocStmt(hdbc,&hstmt) ;
    rc = SQLExecDirect(hstmt,
                 szQuery,
        SQL_NTS)  ;

m_nError = (int) rc ;
    if(rc ==SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
{ /*如果执行成功*/
     SQLTransact(henv,hdbc,SQL_COMMIT);
  SQLFreeStmt(hstmt,SQL_DROP) ;
  return TRUE ;
}
else
{
  m_sErrMsg = "" ;
        while(SQLError(0,
              0,
        hstmt,
        szSQLSTATE,
        &nErr,
        msg,
        sizeof(msg),
        &cbmsg) == SQL_SUCCESS)
       m_sErrMsg = "\t\t" + (CString) msg + "\n" ;
       SQLFreeStmt(hstmt,SQL_DROP) ;
    return FALSE ;
}

}

CString CWdcDatabase::Quote(CString &sText)
{
  CString sResult="" ;
  CString sChar   ;
  int iTextLen = sText.GetLength() ;

  for(int pos = 0 ;pos<iTextLen;pos++)
  {
     sChar = sText.Mid(pos,1) ;
  if (sChar == "'")
   sChar = "''" ;
  sResult += sChar ;
  }

  return  CString("'" + sResult + "'") ;

}

BOOL CWdcDatabase::AddRecord()
{
    unsigned char szSQLSTATE[6] ;
SDWORD nErr ;
unsigned char msg[MAX_ERROR_MSG+1] ;
SWORD cbmsg ;

CString sTemp ;

if(!m_bOpen)
  return FALSE ;

    m_sQuery.Format("Insert Into %s(",m_sTable) ;
    
int ff=0 ;
for(int f=0 ;f<m_nFields;f++)
{
       if(ff>0)
     m_sQuery += "," ;
    ff ++ ;
    m_sQuery += m_sFieldName[f] ;
}

m_sQuery += ") values(" ;
ff = 0 ;
    for(f=0 ;f<m_nFields ;f++)
{
  if(ff>0)
   m_sQuery += "," ;

  ff ++ ;
  /*---如果数据不为空*/
      if(m_sFieldValue[f]!="")
    {
          if(m_bNumeric[f])
           sTemp.Format("%s",m_sFieldValue[f]) ;
   else
     sTemp.Format("%s",Quote(m_sFieldValue[f])) ;
    }
    else
    { /*如果数据为空*/
          sTemp.Format("%s","NULL") ;
    }

  m_sQuery += sTemp ;
}

m_sQuery += ")" ;

memset((char*)szQuery,0x00,sizeof(szQuery)) ;
strcpy((char*)szQuery,m_sQuery) ;
SQLAllocStmt(hdbc,&hstmt) ;
rc = SQLExecDirect(hstmt,
                 szQuery,
        SQL_NTS) ;

m_nError = (int) rc ;
if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
{
     SQLTransact(henv,hdbc,SQL_COMMIT);
        SQLFreeStmt(hstmt,SQL_DROP) ;
  hstmt=NULL ;
  return TRUE ;
}
else
{
  m_sErrMsg = "" ;
        while(SQLError(0,
              0,
        hstmt,
        szSQLSTATE,
        &nErr,
        msg,
        sizeof(msg),
        &cbmsg) == SQL_SUCCESS )

    m_sErrMsg += "\t\t" + (CString) msg +"\n" ;
          SQLFreeStmt(hstmt,SQL_DROP) ;
    return FALSE ;
}
}
/*更新表的指定纪录*/
BOOL CWdcDatabase::UpdateRecord()
{
  unsigned char szSQLSTATE[6] ;
  SDWORD nErr ;
  unsigned char msg[MAX_ERROR_MSG + 1] ;
  SWORD cbmsg ;

  RETCODE rc ;
  CString sTemp ;

  if(!m_bOpen)
   return FALSE ;

  m_sQuery.Format("Update %s Set ",m_sTable) ;

  int ff = 0 ;
  for(int f= 0 ;f<m_nFields ;f++)
  {
     if (ff>0)
   m_sQuery += "," ;
     
  ff ++ ;

   if(m_sFieldValue[f]!="")  /*数据为空*/
   {
     if(m_bNumeric[f])
       sTemp.Format("%s=%s",m_sFieldName[f],m_sFieldValue[f]) ;
     else
       sTemp.Format("%s=%s",m_sFieldName[f],Quote(m_sFieldValue[f])) ;
   }
   else
       sTemp.Format("%s=%s",m_sFieldName[f],NULL) ;

  m_sQuery += sTemp ;
  }
  m_sQuery += " Where " ;

  ff = 0 ;
  for(f=0 ;f<m_nFields ;f++)
  {
   if(m_bKeyField[f]) /*如果为主键*/
   {
         if(ff>0)
    m_sQuery += "," ;
   ff ++ ;
         if(m_bNumeric[f])
    sTemp.Format("%s=%s",m_sFieldName[f],m_sFieldValue[f]) ;
   else
    sTemp.Format("%s=%s",m_sFieldName[f],Quote(m_sFieldValue[f])) ;

   m_sQuery += sTemp ;
   }
  }

  memset((char*)szQuery,0x00,sizeof(szQuery)) ;
  strcpy((char*)szQuery,m_sQuery) ;
  SQLAllocStmt(hdbc,
            &hstmt) ;
  rc = SQLExecDirect(hstmt,
                  szQuery,
      SQL_NTS) ;
  m_nError = (int) rc ;

  if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
  {  
      SQLTransact(henv,hdbc,SQL_COMMIT);
   SQLFreeStmt(hstmt,SQL_DROP) ;
   return TRUE ;
  }
  else
  {  
  m_sErrMsg = "" ;
      while(SQLError(0,
               0,
      hstmt,
      szSQLSTATE,
      &nErr,
      msg,
      sizeof(msg) ,
      &cbmsg ) == SQL_SUCCESS )
      m_sErrMsg += "\t\t" +(CString) msg + "\n" ;
    
   SQLFreeStmt(hstmt,SQL_DROP) ;
   return FALSE ;
  }

}

/* 查找纪录---
返回值
    1: 即没有错误发生的找到纪录
    -1: 在查找过程中出现错误
    0: 查找过程当中没有错误出现,但是没有找到适合条件的纪录,即没有返回纪录  */
int CWdcDatabase::SelectMatchingRecords(CString &sWhereContext)
{
   unsigned char szSQLSTATE[6];
   SDWORD nErr ;
   unsigned char msg[MAX_ERROR_MSG+1] ;
   SWORD cbmsg ;
   CString sTemp ;

   char szData[MAX_DATA] ;
   SDWORD cbData ;
   RETCODE rc ;

   if(!m_bOpen)
    return -1 ;

   sTemp="" ;
   int ff=0 ;
   for(int j=0;j<m_nFields;j++)
   {
       if(ff>0)
     sTemp += "," ;
    ff ++ ;
    sTemp += m_sFieldName[j] ;
   }

   if(sWhereContext == "")
      m_sQuery.Format("Select %s from %s",sTemp,m_sTable) ;
   else
   m_sQuery.Format("Select %s From %s Where %s",sTemp,m_sTable,sWhereContext) ;

   SQLAllocStmt(hdbc,&hstmt_select) ;
   memset((char*)szQuery,0x00,sizeof(szQuery)) ;
   strcpy((char*)szQuery,m_sQuery) ;

   rc = SQLExecDirect(hstmt_select,
                   szQuery,
       SQL_NTS) ;
   m_nError = (int) rc ;

   if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
   {
       rc = SQLFetch(hstmt_select );
       if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    {  /*执行成功,并且有数据*/
      for(int f = 0 ; f<m_nFields ; f++)
   {
               rc = SQLGetData(hstmt_select,
                   f+1 ,
          SQL_C_CHAR,
          szData,
          sizeof(szData) ,
          &cbData ) ;
              
       if(cbData != SQL_NULL_DATA)
    {  /*如果数据不为空*/
                 if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
         m_sFieldValue[f] = CString(szData) ;
        else
       m_sFieldValue[f] = "" ;
    }
    else
     m_sFieldValue[f] = "" ;

   }   

     return 1 ;   /*执行成功,并且有数据*/
    }
    else if(rc == 100)
    {  /*查找成功,但是没有数据*/
          return 0 ;
    }
   }
       m_sErrMsg = "" ;
    while(SQLError(0,
                0,
       hstmt_select,
       szSQLSTATE,
       &nErr,
       msg,
       sizeof(msg),
       &cbmsg) == SQL_SUCCESS )
       m_sErrMsg += "\t\t" +CString(msg) + "\n" ;
     
    SQLFreeStmt(hstmt_select,SQL_DROP) ;
    hstmt_select = NULL ;
    return -1 ;
}

BOOL CWdcDatabase::NextMatchingRecord()
{
   unsigned char szSQLSTATE[6] ;
   SDWORD nErr ;
   unsigned char msg[MAX_ERROR_MSG+1] ;
   SWORD cbmsg ;

   char szData[MAX_DATA] ;
   SDWORD cbData ;
   RETCODE rc ;

   if(!m_bOpen)
    return FALSE ;

   rc = SQLFetch(hstmt_select) ;
   if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
   {
       for(int f =0 ;f<m_nFields ; f++)
    {
          rc = SQLGetData(hstmt_select,
                 f+1 ,
           SQL_C_CHAR,
           szData,
           sizeof(szData),
           &cbData) ;
            if(cbData !=SQL_NULL_DATA)
   { /*如果数据不为空*/
              if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
            m_sFieldValue[f] = (CString) szData ;
        else
       m_sFieldValue[f] = "" ;
   }
   else
    m_sFieldValue[f] = "" ;

    }   
    return TRUE ;
    }

m_sErrMsg = "" ;
    while(SQLError(0,
             0,
       hstmt_select,
       szSQLSTATE,
       &nErr,
       msg,
       sizeof(msg),
       &cbmsg )==SQL_SUCCESS )
            
           m_sErrMsg += "\t\t" +CString (msg) + "\n" ;
        SQLFreeStmt(hstmt_select,SQL_DROP) ;
     hstmt_select = NULL ;

   return FALSE ;
}
/*统计符合条件的纪录总数
  返回值
  -1: 执行出错
  >0: 执行成功,并且返回统计结果 */
long CWdcDatabase::GetRecordCount(CString &sSel)
{
   unsigned char szSQLSTATE[6] ;
   SDWORD nErr ;
   unsigned char ErrMsg[500] ;
   SWORD cbmsg;
   long lRet ;  /*返回值*/

   unsigned char lpSelect[1000];
   memset((char*)lpSelect,0x00,sizeof(lpSelect)) ;
   if(sSel=="")
       sTemp.Format("Select count(*) from %s ",m_sTable) ;
   else
    sTemp.Format("Select Count(*) From %s Where %s",m_sTable,sSel) ;

   if(!m_bOpen)
    return -1  ;
   strcpy((char*)lpSelect,sTemp) ;
   SQLAllocStmt(hdbc,&hstmt_select) ;
   rc=SQLExecDirect(hstmt_select,
              lpSelect,
     SQL_NTS) ;
   if(rc==SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
   { /* SQLExecDirect True*/
       SQLBindCol(hstmt_select,1,SQL_INTEGER,&lRet,sizeof(int),NULL) ;
    rc = SQLFetch(hstmt_select) ;
    if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    { /*有数据*/
    SQLFreeStmt(hstmt_select,SQL_DROP) ;
    hstmt_select = NULL ;
          return lRet ;
    }
    else if (rc == 100)
    { /*没有数据*/
    SQLFreeStmt(hstmt_select,SQL_DROP) ;
    hstmt_select = NULL ;
          return 0 ;
    }
       else if (rc < 0)
    { /*出错*/
         goto end ;
    }
   }
   else
   { /* SQLExecDirect FALSE */
    goto end ;
  }

end:
  {
      m_sErrMsg = "" ;
         while(SQLError(0,
              0,
        hstmt_select,
                       szSQLSTATE,
        &nErr,
        ErrMsg,
        sizeof(ErrMsg),
        &cbmsg)==SQL_SUCCESS)
         m_sErrMsg += "\t\t" + CString(ErrMsg) +"\n" ;  

   SQLFreeStmt(hstmt_select,SQL_DROP) ;
   hstmt_select = NULL ;
    return -1 ;
  }
}
/*非常有用函数,用在确保语句句柄释放*/
void CWdcDatabase::DropHstmtSelect()
{
if(hstmt_select !=NULL)
{
  SQLFreeStmt(hstmt_select,SQL_DROP) ;
  hstmt_select = NULL ;
}
}

原文转自:http://www.ltesting.net