您好,欢迎访问一九零五行业门户网

在VS环境下以ADO方式操作Oracle数据库

利用ado引擎方式访问oracle数据库的实现方法: 定义数据库头文件为cdboperation.h #pragma once #import c:\program files\com
利用ado引擎方式访问oracle数据库的实现方法:
定义数据库头文件为cdboperation.h
#pragma once 
#import c:\program files\common files\system\ado\msado15.dll no_namespace rename(eof,adoeof),rename(locktypeenum,adolocktypeenum),rename(datatypeenum,adodatatypeenum),\
rename(fieldattributeenum,adofieldattributeenum),rename(editmodeenum,adoeditmodeenum),rename(recordstatusenum,adorecordstatusenum),rename(parameterdirectionenum,adoparameterdirectionenum)
class cdboperation 

 public: 
  //初始化数据库操作需要的对象 
  cdboperation(void); 
  ~cdboperation(void);
//连接至数据库 
  bool conntodb();
//数据库操作函数 
  //查询操作 删除以及添加 
  _recordsetptr executewithressql(const char *);
private: 
  void printerrorinfo(_com_error &);
private: 
  //初始化数据库连接、命令、记录集 
  _connectionptr createconnptr(); 
  _commandptr createcommptr(); 
  _recordsetptr createrecsetptr();
private: 
  //数据库连接需要的连接、命令操作对象 
  _connectionptr m_pconnection; 
  _commandptr m_pcommand; 
}; 
定义程序源文件cdboperation.cpp
#include cdboperation.h 
cdboperation::cdboperation(void) 

    coinitialize(null); 
    m_pconnection = createconnptr(); 
    m_pcommand = createcommptr(); 

cdboperation::~cdboperation(void) 

    m_pconnection->close(); 

bool cdboperation::conntodb() 

    if (null == m_pconnection) 
    { 
        printf(failed to create connection\n); 
        return false; 
    } 
    try 
    { 
  _bstr_t strconnect=provider=oraoledb.oracle;user id=scott;password=19900624;persist security info=true;data source=\(description =(address_list =(address = (protocol = tcp)(host = 10.108.85.137)(port = 1521)) )(connect_data = (sid = orcl)(server=dedicated)))\;
    hresult hr=m_pconnection->open(strconnect,,,null);
        if (true == failed(hr)) 
        { 
            return false; 
        } 
        m_pcommand->activeconnection = m_pconnection; 
        return true; 
    } 
    catch(_com_error &e) 
    { 
        printerrorinfo(e); 
        return false; 
    } 

_recordsetptr cdboperation::executewithressql(const char *sql) 

    try 
    { 
        m_pcommand->commandtext = _bstr_t(sql); 
        _recordsetptr prst = m_pcommand->execute(null, null, adcmdtext); 
        return prst; 
    } 
    catch(_com_error &e) 
    { 
        printerrorinfo(e); 
        return null; 
    } 

void cdboperation::printerrorinfo(_com_error &e) 

    printf(error infomation are as follows\n); 
    printf(errorno: %d\nerror message:%s\nerror source:%s\nerror description:%s\n, e.error(), e.errormessage(), (lpctstr)e.source(), (lpctstr)e.description()); 
}
_connectionptr cdboperation::createconnptr() 

    hresult hr; 
    _connectionptr connptr; 
    hr = connptr.createinstance(__uuidof(connection)); 
    if (failed(hr) == true) 
    { 
        return null; 
    } 
    return connptr; 
}
_commandptr cdboperation::createcommptr() 

    hresult hr; 
    _commandptr commptr; 
    hr = commptr.createinstance(__uuidof(command)); 
    if (failed(hr) == true) 
    { 
        return null; 
    } 
    return commptr; 
}
_recordsetptr cdboperation::createrecsetptr() 

    hresult hr; 
    _recordsetptr recsetptr; 
    hr = recsetptr.createinstance(__uuidof(recordset)); 
    if (failed(hr) ==true) 
    { 
        return null; 
    } 
    return recsetptr; 
}

其它类似信息

推荐信息