How to: Fetch Columns Using IRow::GetColumns (or IRow::Open) and ISequentialStream
Large data can be bound or retrieved using the ISequentialStream interface. For bound columns, the status flag DBSTATUS_S_TRUNCATED indicates that the data is truncated.
The complete sample code is in this file FetchColumns_A.cpp. You can download an archive containing the sample from the SQL Server Downloads page on MSDN.
This sample was developed using Microsoft Visual C++ 2005.
The code below creates the sample table used by the application.
USE AdventureWorks2008R2;
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
/* Enter data. */
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
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 crypto API. |
To fetch columns using IRow::GetColumns (or IRow::Open) and ISequentialStream
Establish a connection to the data source.
Execute the command (in this example, ICommandExecute::Execute() is called with IID_IRow).
Fetch the column data using IRow::Open() or IRow::GetColumns().
IRow::Open() can be used to open an ISequentialStream on the row. Specify DBGUID_STREAM to indicate that the column contains a stream of binary data (IStream or ISequentialStream can then be used to read the data from the column).
If IRow::GetColumns() is used, the pData element of DBCOLUMNACCESS structure is set to point to a stream object.
Use ISequentialStream::Read() repeatedly to read the specified number of bytes into the consumer buffer.