代码改变世界

_com_error e

2012-10-24 11:11  江上渔者  阅读(598)  评论(0编辑  收藏  举报
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "adoEOF") //引用ado组件
#include  <stdio.h> 
#include  <icrsint.h> 
#include  "iostream.h" 

class CCustomRs : public CADORecordBinding 
{ 
BEGIN_ADO_BINDING(CCustomRs) 
   ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_szau_lname,  
         sizeof(m_szau_lname), lau_lnameStatus, false) 
   ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_szau_fname,  
         sizeof(m_szau_fname), lau_fnameStatus, false) 
   ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_szphone,     
         sizeof(m_szphone),    lphoneStatus,    true) 
END_ADO_BINDING()

public: 
   CHAR   m_szau_lname[41]; 
   ULONG  lau_lnameStatus; 
   CHAR   m_szau_fname[41]; 
   ULONG  lau_fnameStatus; 
   CHAR   m_szphone[12]; 
   ULONG  lphoneStatus; 
};

 
VOID main() 
{ 

    ::CoInitialize(NULL); 

    _ConnectionPtr pConn; //定义连接
    pConn.CreateInstance(__uuidof(Connection));   //实例化
  
    _RecordsetPtr pRs  = NULL; //定义记录集
    _CommandPtr  pCmd  = NULL; //定义command

    pConn-> Open("Provider=SqlOleDb;Server=(local);Database=tst","sa","xxx",adConnectUnspecified); //打开连接

    pRs.CreateInstance(__uuidof(Recordset)); //实例化
    pCmd.CreateInstance(__uuidof(Command)); 
    pCmd-> CommandText = "raiserror ('该借阅证本日借阅书籍已超过3本,请改日再借',16,10) "; //命令字串,要执行的语句


    try
    {
        pCmd-> PutActiveConnection(_variant_t((IDispatch*)pConn)); //将pConn赋为command的活动连接
        pRs = pCmd-> Execute(NULL,NULL,adCmdText); //用cmd.execute方法得到记录集
        pRs-> Close();  
        pConn-> Close(); 
    }
    catch(_com_error& e) 
    {
        cout << "错误信息:" << e.ErrorMessage() <<endl;
        cout << "错误源:" << e.Source() <<endl;
        cout << "错误描述:" << e.Description()  <<endl;

    }

  
    CoUninitialize(); 
}
/*
结果
--------------------------
错误信息: IDispatch error #3092
错误源: microsoft oledb provider for sql server
错误描述:该借阅证本日借阅书籍已超过3本,请改日再借

*/