- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- OLE DB Architecture
- Developing an OLE DB Application
- Retrieving Column Information
- Using Transactions
- Using Enumerators
- Summary
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
Retrieving Column Information
After you have created a rowset object, you can retrieve information about the columns in the rowset, including the column IDs, data types, updatability, and other info. This capability is especially important if you don't quite know the column names or types, as you may not with a SELECT * command. This section describes how to retrieve column information from a recordset.
GetColumnInfo()
You can call the GetColumnInfo() method to retrieve information about the columns in the rowset. The prototype for GetColumnInfo() is shown here:
HRESULT GetColumnInfo (ULONG * pcColumns, DBCOLUMNINFO ** prgInfo,
OLECHAR ** ppStringsBuffer);
GetColumnInfo() returns the number of columns included in the rowset at pcColumns. In addition, it returns a pointer to an array of DBCOLUMNINFO structures—one for each column—at prgInfo, and returns a pointer to a buffer containing all string values associated with the columns (such as column names) at ppStringsBuffer. Both these buffers are allocated by OLE DB and should be freed with a call to IMalloc::Free() when you are finished with them.
DBCOLUMNINFO
When GetColumnInfo() is called, a pointer to an array of DBCOLUMNINFO structures is returned at prgInfo. This array includes a DBCOLUMNINFO structure for each column in the rowset, possibly including a bookmark column.
The DBCOLUMNINFO structure returned from GetColumnInfo() is shown here:
typedef struct tagDBCOLUMNINFO {
LPOLESTR pwszName;
ITypeInfo * pTypeInfo;
ULONG iOrdinal;
DBCOLUMNFLAGS dwFlags;
ULONG ulColumnSize;
DBTYPE wType;
BYTE bPrecision;
BYTE bScale;
DBID columnid;
} DBCOLUMNINFO;
The pwszName field points to a string containing the column name. The memory for this string is allocated by OLE DB in the block of memory specified by the pointer returned in ppStringsBuffer when GetColumnInfo() is called. This way, you can access each of the column names simply by using the pwszName pointer, but you need only call IMalloc::Free() for the one pointer returned in ppStringsBuffer.
In the current release, pTypeInfo is reserved and should always return NULL, whereas the iOrdinal field returns the ordinal for the column. The bookmark column is column 0, and the others are numbered in order, starting with 1. The column ID for the column is returned in the columnid field.
The dwFlags field contains a bitmap that describes the characteristics of the column and can contain a combination of the following values:
- DBCOLUMNFLAGS_CACHEDEFERRED is set if the column's data is cached, as determined by setting the DBPROP_CACHEDEFERRED property for the rowset.
- DBCOLUMNFLAGS_ISBOOKMARK is set if the column contains a bookmark.
- DBCOLUMNFLAGS_ISFIXEDLENGTH is set if all values for this column are the same length.
- DBCOLUMNFLAGS_ISLONG is set if the column contains long data, which is best retrieved by using one of the storage interfaces, although you can also retrieve the data with IRowset::GetData().
- DBCOLUMNFLAGS_ISNULLABLE is set if you are allowed to set the column to NULL.
- DBCOLUMNFLAGS_ISROWID is set if the column contains a rowid, which is used only to identify the row and cannot be written to.
- DBCOLUMNFLAGS_ISROWVER is set if this column is used only for a provider-specific versioning scheme.
- DBCOLUMNFLAGS_MAYBENULL is set if the column can return a NULL value. In certain cases, such as with outer joins, this attribute can be different than DBCOLUMNFLAGS_ISNULLABLE.
- DBCOLUMNFLAGS_MAYDEFER is set if the data is not fetched from the data source until it is retrieved with a call to IRowset::GetData(). You can set this attribute by setting the DBPROP_DEFERRED property in the rowset property group.
- DBCOLUMNFLAGS_WRITE is set if the column can be written to by calling IRowsetChange::SetData().
- DBCOLUMNFLAGS_WRITEUNKNOWN is set if it is not known whether the column can be written.
The ulColumnSize field contains the maximum length of the column's data. For columns of type DBTYPE_STR or DBTYPE_WSTR, this is given in characters (which are 2 bytes). For columns of type DBTYPE_BYTES, the maximum length is given in bytes. If there is not a maximum length, ulColumnSize is set to 0xFFFFFFFF.
The wType field gives the type of the column data, and the bPrecision field gives the maximum precision for columns with numeric data types. If the column does not hold a numeric value, bPrecision is set to 0xFFFFFFFF. In addition, the bScale field gives the number of digits to the right of the decimal point.
GetColumnsRowset()
In addition to GetColumnInfo(), you can retrieve more complete information about the columns in a rowset by calling GetColumnsRowset(). GetColumnsRowset() returns a rowset object, which can contain a wide variety of optional information about the columns, as well as the information returned by GetColumnInfo(). However, not all providers implement this functionality.
Using Transactions | Next Section

Account Sign In
View your cart