While persistence of rowsets to XML is available through ADO, OLE DB does not provide a service for this. Similarly, OLE DB doesn't provide a service for loading an OLE DB rowset from an XML file object. To persist an OLE DB rowset as XML data, use the ADO IADORecordsetConstruction interface with an ADO Recordset object that wraps an OLE DB rowset.
The sample code provided in this section shows How To
persist an OLE DB rowset to an XML file.
load OLE DB rowsets from an XML file
The way to persist an OLE DB rowset to an external XML file is shown in the Rowset2Xml() function, which takes a rowset and transfers it to an ADO recordset object. It then uses the Save() method on the recordset object to save the rowset to an external XML file.
Loading of OLE DB rowsets from an external XML file is demonstrated in the Xml2Rowset() function, which creates an ADO recordset object and uses the Open method to open the file. Then it retrieves the rowset out of the recordset object by using the get_Rowset() method of the IADORecordsetConstruction interface.
The code uses a Microsoft SQL Server datasource. To run the sample, you can use the following script to create a table "books" in the "pubs" database.
CREATE TABLE Books (
id int IDENTITY (1, 1) NOT NULL ,
Title char(50) ,
Publisher char(50)
) ON [PRIMARY]
GO
INSERT INTO books(Title, Publisher) VALUES ('Debugging applications','MS Press')
GO
INSERT INTO books(Title, Publisher) VALUES ('Inside SQL Server 7.0','MS Press')
GO
For simplicity, the code does not check all of the returned HRESULT. It's a good practice to check the HRESULT and use CDBErrorInfo/AtlTraceErrorRecords() to get back more descriptive errors.
In Rowset2Xml()function,
hr = cmdBooks.Open();
is called to create the CCommand and execute the select statement, and then retrieve the CRowset associated with this CCommand. Alternatively, you can explicitly get the IRowset back by calling Execute as follows: