Fetching Columns Using IRow::GetColumns (OLE DB)
New Information - SQL Server 2000 SP3.
The IRow interface allows direct access to columns of a single row in the result set. Thus, IRow is an efficient way to retrieve columns from a result set with one row.
To fetch columns using IRow::GetColumns
- Establish a connection to the data source.
- Execute the command (in the following example, ICommandExecute::Execute() is called with IID_IRow).
- Execute IRow::GetColumns() to fetch one or more columns in the resulting row. If you want to find the actual column size before fetching data, set the pData in DBCOLUMNACCESS to NULL. The call to IRow::GetColumns() returns only the column width. Another call the IRow::GetColumns() will fetch the data.
- Execute IRow::GetColumns() until all the columns you need are accessed. The columns must be accessed in sequence.
This example shows how to fetch a single row using IRow. It also illustrates two ways to access columns in the row:
- Fetching columns in groups
- Obtaining the column width first and then fetching the column data.
Here is the sample table used by the application:
USE pubs GO IF EXISTS (SELECT name FROM sysobjects WHERE name = 'MyTable') DROP TABLE MyTable GO CREATE TABLE MyTable ( col1 int, col2 varchar(50), col3 char(50), col4 datetime, col5 float, col6 money, col7 sql_variant, col8 binary(50), col9 text, col10 image ) GO INSERT INTO MyTable values ( 10, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '11/1/1999 11:52 AM', 3.14, 99.95, CONVERT(nchar(50), N'AbCdEfGhIjKlMnOpQrStUvWxYz'), 0x123456789, REPLICATE('AAAAABBBBB', 500), REPLICATE(0x123456789, 500) ) GO
The complete sample code is in this file: FetchColumns_B.cpp. You can download an archive containing the sample from the Microsoft® SQL Server™ Downloads page at this Microsoft Web site.
This sample was developed with Microsoft Visual C++® version 6.0, and may expose properties of the Microsoft Foundation Classes.
Security Note When possible, use Windows Authentication. If Windows Authentication is not available, prompt users to enter their credentials at run time. Avoid storing credentials in a file. If you must persist credentials, you should encrypt them with the Win32 cryptoAPI.