10、邦定数据
定义一个绑定类,将其成员变量绑定到一个指定的记录集,以方便于访问记录集的字段值。
(1). 从CADORecordBinding中派生出一个类:
class CCustomRs : public CADORecordBinding { BEGIN_ADO_BINDING(CCustomRs) ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_szau_fname, sizeof(m_szau_fname), lau_fnameStatus, false) ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_szau_lname, sizeof(m_szau_lname), lau_lnameStatus, false) ADO_VARIABLE_LENGTH_ENTRY2(4, adVarChar, m_szphone, sizeof(m_szphone), lphoneStatus, true) END_ADO_BINDING() public: CHAR m_szau_fname[22]; ULONG lau_fnameStatus; CHAR m_szau_lname[42]; ULONG lau_lnameStatus; CHAR m_szphone; ULONG lphoneStatus; }; |
class CADORecordBinding { public: STDMETHOD_(const ADO_BINDING_ENTRY*, GetADOBindingEntries) (VOID) PURE; }; BEGIN_ADO_BINDING宏的定义也在icrsint.h文件里,内容是: #define BEGIN_ADO_BINDING(cls) public: \ typedef cls ADORowClass; \ const ADO_BINDING_ENTRY* STDMETHODCALLTYPE GetADOBindingEntries() { \ static const ADO_BINDING_ENTRY rgADOBindingEntries = { ADO_VARIABLE_LENGTH_ENTRY2宏的定义也在icrsint.h文件里: #define ADO_VARIABLE_LENGTH_ENTRY2(Ordinal, DataType, Buffer, Size, Status, Modify)\ {Ordinal, \ DataType, \ 0, \ 0, \ Size, \ offsetof(ADORowClass, Buffer), \ offsetof(ADORowClass, Status), \ 0, \ classoffset(CADORecordBinding, ADORowClass), \ Modify}, #define END_ADO_BINDING宏的定义也在icrsint.h文件里: #define END_ADO_BINDING() {0, adEmpty, 0, 0, 0, 0, 0, 0, 0, FALSE}};\ return rgADOBindingEntries;} |
_RecordsetPtr Rs1; IADORecordBinding *picRs=NULL; CCustomRs rs; Rs1->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*)&picRs)); picRs->BindToRecordset(&rs); |
//Set sort and filter condition: // Step 4: Manipulate the data Rs1->Fields->GetItem("au_lname")->Properties->GetItem("Optimize")->Value = true; Rs1->Sort = "au_lname ASC"; Rs1->Filter = "phone LIKE ''415 5*''"; Rs1->MoveFirst(); while (VARIANT_FALSE == Rs1->EndOfFile) { printf("Name: %s\t %s\tPhone: %s\n", (rs.lau_fnameStatus == adFldOK ? rs.m_szau_fname : ""), (rs.lau_lnameStatus == adFldOK ? rs.m_szau_lname : ""), (rs.lphoneStatus == adFldOK ? rs.m_szphone : "")); if (rs.lphoneStatus == adFldOK) strcpy(rs.m_szphone, "777"); TESTHR(picRs->Update(&rs)); // Add change to the batch Rs1->MoveNext(); } Rs1->Filter = (long) adFilterNone; if (picRs) picRs->Release(); Rs1->Close(); pConn->Close(); |