打印本文 打印本文 关闭窗口 关闭窗口
VC++程序员应当如何阅读ADO文档
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4430  更新时间:2009/4/23 10:41:00  文章录入:mintao  责任编辑:mintao

adFldNull

3

读字段值时,指示一个空值被返回。写字段值时,指示当字段自身无法编码NULL时该字段将被设置为NULL。

adFldTruncated

4

变长数据或数字被截断。

adFldSignMismatch

5

值是有符号数,而数据类型是无符号数。

adFldDataOverFlow

6

数据值超出界限。

adFldCantCreate

7

不知名的列类型和字段已经被打开。

adFldUnavailable

8

字段值无法确定。比如一个新的未赋值的无缺省值的字段。

adFldPermissionDenied

9

未被允许更新数据。

adFldIntegrityViolation

10

更新字段时值违反了列的完整性要求。

adFldSchemaViolation

11

更新字段时值违反了列的规范要求。

adFldBadStatus

12

更新字段时,无效的状态参数。

adFldDefault

13

更新字段时,使用缺省值。

 

使用VC++对ADO的扩展的示例

在这个例子中,还使用了COM专有的“智能指针”功能,它能自动处理IADORecordBinding接口的QueryInterface和引用计数。如果没有智能指针,你得这样编码:

IADORecordBinding   *picRs = NULL;

...

TESTHR(pRs->QueryInterface(

          __uuidof(IADORecordBinding), (LPVOID*)&picRs));

...

if (picRs) picRs->Release();

使用智能指针,你可以用这样的语句从IADORecordBinding接口派生IADORecordBindingPtr类型:

_COM_SMARTPTR_TYPEDEF(IADORecordBinding, __uuidof(IADORecordBinding));

然后这样实例化指针:

IADORecordBindingPtr picRs(pRs);

因为VC++的扩展由Recordset对象实现,因此智能指针picRs的构造函数使用了_RecordsetPtr类指针pRs。构造函数利用pRs调用QueryInterface来获得IADORecordBinding接口。

 

// 以下即是示例程序

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \

   no_namespace rename("EOF", "EndOfFile")

 

#include <stdio.h>

#include <icrsint.h>

_COM_SMARTPTR_TYPEDEF(IADORecordBinding, __uuidof(IADORecordBinding));

 

inline void TESTHR(HRESULT _hr) { if FAILED(_hr) _com_issue_error(_hr); }

 

class CCustomRs : public CADORecordBinding

{

BEGIN_ADO_BINDING(CCustomRs)

   ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_ch_fname,

                        sizeof(m_ch_fname), m_ul_fnameStatus, false)

   ADO_VARIABLE_LENGTH_ENTRY2(4, adVarChar, m_ch_lname,

                        sizeof(m_ch_lname), m_ul_lnameStatus, false)

END_ADO_BINDING()

public:

   CHAR    m_ch_fname[22];

   CHAR    m_ch_lname[32];

   ULONG   m_ul_fnameStatus;

   ULONG   m_ul_lnameStatus;

};

 

void main(void)

{

   ::CoInitialize(NULL);

   try

      {

      _RecordsetPtr pRs("ADODB.Recordset");

      CCustomRs rs;

      IADORecordBindingPtr picRs(pRs);

     

      pRs->Open("SELECT * FROM Employee ORDER BY lname",

         "dsn=pubs;uid=sa;pwd=;",

         adOpenStatic, adLockOptimistic, adCmdText);

     

      TESTHR(picRs->BindToRecordset(&rs));

 

      while (!pRs->EndOfFile)

         {

      // 处理CCustomRs中的数据

         printf("Name = %s %s\n",

            (rs.m_ul_fnameStatus == adFldOK ? rs.m_ch_fname: "<Error>"),

            (rs.m_ul_lnameStatus == adFldOK ? rs.m_ch_lname: "<Error>"));

 

      // 移动到下一行,新行的值会被自动填充到对应的CCustomRs的变量中

         pRs->MoveNext();

         }

      }

   catch (_com_error &e )

      {

      printf("Error:\n");

      printf("Code = %08lx\n", e.Error());

      printf("Meaning = %s\n", e.ErrorMessage());

      printf("Source = %s\n", (LPCSTR) e.Source());

      printf("Description = %s\n", (LPCSTR) e.Description());

      }

   ::CoUninitialize();

}

 

 

上一页  [1] [2] [3] [4] [5] [6] 

打印本文 打印本文 关闭窗口 关闭窗口