Microsoft KB Archive/262450

= HOWTO: A C++ Sample of ADO Recordset XML Persistence =

Article ID: 262450

Article Last Modified on 11/4/2003

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft XML Parser 2.5
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q262450



SUMMARY
With ADO 2.5 and later, Recordset objects can be persisted into any object that implements the IStream interface. The sample code in this article shows how to persist/load ADO Recordset to/from an external xml file, ADO IStream Object and XML DOM object.



MORE INFORMATION
1. The sample uses authors table in the pubs database of SQL Server Database.

2. Modify the connection string to supply the right datasource name and user credentials.

3. Since there are two #import dll, for simplicity of programing, I rename the ADO(ADODB)namespace as MSXML, and use MSXML for the interfaces defined in both dlls. Alternatively, you can always prefix the interface with appropriate namespace without doing this.

Note You must change User ID= and Password = to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database. // 1. ADO Recordset <-> external xml file // 2. ADO Recordset <-> ADO IStream Object // 3. ADO Recordset <-> DOM Document


 * 1) import &quot;C:\Program files\Common Files\System\Ado\msado15.dll&quot; rename_namespace(&quot;MSXML&quot;) rename(&quot;EOF&quot;, &quot;ADOEOF&quot;)

using namespace MSXML;
 * 1) import &quot;c:\winnt\system32\msxml.dll&quot;

void dump_error(_com_error &e) ; //exception handling
 * 1) include &quot;stdio.h&quot;
 * 2) include &quot;io.h&quot;

void main {   HRESULT hr; CoInitialize(NULL);

try {       //open the connection, get the reocrdset ready _ConnectionPtr pConn; _RecordsetPtr pRs; hr = pConn.CreateInstance(__uuidof(Connection)); hr = pRs.CreateInstance(__uuidof(Recordset)); pConn->CursorLocation = adUseClient; _bstr_t strConn(&quot;Provider=sqloledb;Data Source=juliaj01;Initial Catalog=pubs;User Id= ;Password= ;&quot;); hr = pConn->Open(strConn, &quot; &quot;, &quot; &quot;, adConnectUnspecified); hr = pRs->Open(&quot;SELECT * from authors&quot;, pConn.GetInterfacePtr, adOpenForwardOnly, adLockReadOnly, adCmdText); //preparation to save RS as xml file, struct _finddata_t xml_file; long hFile; if( (hFile = _findfirst(&quot;authors.xml&quot;, &xml_file )) != -1L) {           DeleteFile(&quot;authors.xml&quot;); //if the file exists, delete it        }

// 1. Persist it to an external xml file by calling Save with file name and adPersistXML hr = pRs->Save(&quot;authors.xml&quot;, adPersistXML); // 2. Persist it to ADO IStream Object _StreamPtr pStream ; //declare one first pStream.CreateInstance(__uuidof(Stream)); //create it after hr = pRs->Save(pStream.GetInterfacePtr, adPersistXML);   //old trick, call Save // 3. Persist it to DOM Document IXMLDOMDocumentPtr pXMLDOMDoc; pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument)); hr = pRs->Save(pXMLDOMDoc.GetInterfacePtr, adPersistXML); // if you want to check out the content call printf(pXMLDOMDoc->Getxml);

//Recycle the Recordset object hr = pRs->Close;

// 4. load the recordset back from the file by calling Open with MSPersist provider and adCmdFile. // the Recordset will be a ReadOnly, Forwardly only hr = pRs->Open(&quot;authors.xml&quot;,&quot;Provider=MSPersist;&quot;,adOpenForwardOnly,adLockReadOnly,adCmdFile); hr = pRs->Close;

// 5. Load from IStream object, call Open, first param is pStream.GetInterfacePtr

// Set the steam object position to the beginning of the stream: pStream->Position = 0;

// call Open,  passing in vtMissing for connection string, see Q245485 for details hr = pRs->Open(pStream.GetInterfacePtr,vtMissing, adOpenForwardOnly,adLockReadOnly,adCmdFile); hr = pRs->Close; // 6 .Load from DOM Document, call Open, first param is pXMLDOMDoc.GetInterfacePtr and // pass in vtMissing for connection string, see Q245485 hr = pRs->Open(pXMLDOMDoc.GetInterfacePtr, vtMissing, adOpenForwardOnly, adLockReadOnly, adCmdFile);

hr = pRs->Close;

//Don't forget to clean up the stream object hr = pStream->Close; }   catch(_com_error &e) {       dump_error(e); }  }

void dump_error(_com_error &e) {   _bstr_t bstrSource(e.Source); _bstr_t bstrDescription(e.Description); // Print Com errors. printf(&quot;Error\n&quot;); printf(&quot;\tCode = %08lx\n&quot;, e.Error); printf(&quot;\tCode meaning = %s&quot;, e.ErrorMessage); printf(&quot;\tSource = %s\n&quot;, (LPCSTR) bstrSource); printf(&quot;\tDescription = %s\n&quot;, (LPCSTR) bstrDescription); }

Note The sample code provided in this article contains a reference to MSXML 2.5 or earlier. If a newer version of MSXML has been installed in replace mode on your machine, the sample code will automatically use this new version. If a newer version of MSXML has been installed in side-by-side mode on your machine, the code may use the older version.

To run the code with MSXML 3.0, following lines of code need to be changed:  Replace:


 * 1) import &quot;C:\Program files\Common Files\System\Ado\msado15.dll&quot; rename_namespace(&quot;MSXML&quot;) rename(&quot;EOF&quot;, &quot;ADOEOF&quot;)


 * 1) import &quot;c:\winnt\system32\msxml.dll&quot;

using namespace MSXML;

With:


 * 1) import &quot;C:\Program files\Common Files\System\Ado\msado15.dll&quot; rename_namespace(&quot;MSXML2&quot;) rename(&quot;EOF&quot;, &quot;ADOEOF&quot;)


 * 1) import &quot;c:\winnt\system32\msxml3.dll&quot; using namespace MSXML2;

 Replace:

pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));

With:

pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument30));



To run the code with MSXML 4.0, following lines of code need to be changed:  Replace:


 * 1) import &quot;C:\Program files\Common Files\System\Ado\msado15.dll&quot; rename_namespace(&quot;MSXML&quot;) rename(&quot;EOF&quot;, &quot;ADOEOF&quot;)


 * 1) import &quot;c:\winnt\system32\msxml.dll&quot; using namespace MSXML;

With:


 * 1) import &quot;C:\Program files\Common Files\System\Ado\msado15.dll&quot; rename_namespace(&quot;MSXML2&quot;) rename(&quot;EOF&quot;, &quot;ADOEOF&quot;)


 * 1) import &quot;c:\winnt\system32\msxml4.dll&quot; using namespace MSXML2;

 Replace:

pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));

With:

pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument40));



