Microsoft Knowledge Base Email Alertz

KBAlertz.com: (262450) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 262450 - Last Review: November 4, 2003 - Revision: 2.0

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

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=<username> and Password =<strong 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

#import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML")  rename("EOF", "ADOEOF")

#import "c:\winnt\system32\msxml.dll" 
using namespace MSXML;

#include "stdio.h"
#include "io.h"
void dump_error(_com_error &e) ; //exception handling

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("Provider=sqloledb;Data Source=juliaj01;Initial Catalog=pubs;User Id=<username>;Password=<strong password>;");
		hr = pConn->Open(strConn, "<username>", "<strong password>", adConnectUnspecified);
		hr = pRs->Open("SELECT * from authors", pConn.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
			
		//preparation to save RS as xml file, 
		struct _finddata_t xml_file;
		long hFile;
		if( (hFile = _findfirst("authors.xml", &xml_file )) != -1L)
		{
			DeleteFile("authors.xml"); //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("authors.xml", 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("authors.xml","Provider=MSPersist;",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("Error\n");
	printf("\tCode = %08lx\n", e.Error());
	printf("\tCode meaning = %s", e.ErrorMessage());
	printf("\tSource = %s\n", (LPCSTR) bstrSource);
	printf("\tDescription = %s\n", (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:
    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML") rename("EOF", "ADOEOF")

    #import "c:\winnt\system32\msxml.dll"
    using namespace MSXML;
    With:
    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML2") rename("EOF", "ADOEOF")

    #import "c:\winnt\system32\msxml3.dll" 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:
    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML") rename("EOF", "ADOEOF")

    #import "c:\winnt\system32\msxml.dll" using namespace MSXML;
    With:
    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML2") rename("EOF", "ADOEOF")

    #import "c:\winnt\system32\msxml4.dll" using namespace MSXML2;
  • Replace:
    pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));
    With:
    pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument40));

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
245485  (http://kbalertz.com/Feedback.aspx?kbNumber=245485/EN-US/ ) PRB: Load From Stream Fails When Specifying MSPersist Provider
262447  (http://kbalertz.com/Feedback.aspx?kbNumber=262447/EN-US/ ) HOWTO: Persist/Load OLEDB Rowset to/from XML with ATL consumer template classes
259555  (http://kbalertz.com/Feedback.aspx?kbNumber=259555/EN-US/ ) PRB: Error Occurs When You Open an ADO Recordset on an XML Stream

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++, 32-bit Learning Edition 6.0
  • Microsoft XML Parser 2.5
  • Microsoft XML Parser 2.6
  • Microsoft XML Parser 3.0
Keywords: 
kbhowto KB262450
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please