PostHeaderIcon ADO RecordSet To XML

ADO is one of the old technique in VC++ DB,but I like it that way in project~

Today, I’ll introduce it with XML, a popular format file for us you know.

In project with C/S, maybe it is old for you because the popular B/S, such as JavaEE, Groovy,

and I also a developer of B/S use Java, I hava to do some project with C/S, I prefer C++ to do

them, and I love C++ but no reasons.

OK,let’s begin. Our platform is Windows because of our client.

With ADO, you have to use “msado15.dll” or newer version of it;

With XML, you have to use “msxml6.dll” or newer version of it.

in your project of “stdafx.h” file, you can declare like the below:


#import "msado15.dll" no_namespace rename("EOF", "ADOEOF")
#import "msxml6.dll" rename_namespace("MSXML2")
using namespace MSXML2;

To reach your target, you can declare a function named “sql_to_xml“,

just like this:

 /* make a sql to xml file */
BOOL sql_to_xml(CString& sql);

Here is the implemention of it , we use MySQL database for it,

other DB is also same with this one:

BOOL CWorkerSocket::sql_to_xml( CString& sql )
{
	HRESULT hr;
	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;

		/* MySQL driver, username, password */ 

		_bstr_t _uid="root";
		_bstr_t _pwd="654321";
		_bstr_t strConn="Driver=MySQL ODBC 5.1 Driver;SERVER=";

		/* conn path */
		strConn+="localhost";
		strConn+=";DATABASE=";
		strConn+="test";
		strConn+=";UID=";
		strConn+=_uid;
		strConn+=";PWD=";
		strConn+=_pwd;
		strConn+=";";

		/* open conn */
		hr = pConn->Open(strConn, _uid, _pwd, adConnectUnspecified);
		if(FAILED(hr)) return FALSE;
		_bstr_t _sql=sql;

		/* execute sql */
		hr = pRs->Open(_sql,
			pConn.GetInterfacePtr(),
			adOpenForwardOnly,
			adLockReadOnly,
			adCmdText);
		if(FAILED(hr))
		{
			AfxMessageBox("db conn error");
			return FALSE;
		}
		/* save preparation to save RS as xml file */
		struct _finddata_t xml_file;
		long hFile;
		if( (hFile = _findfirst(m_strFileName, &xml_file )) != -1L)
		{
			DeleteFile(m_strFileName); /* if the file exists, delete it */
		}

		/* Persist it to an external xml file by calling Save with file name and adPersistXML */
		_variant_t temp_filename=(LPCTSTR)m_strFileName;
		hr = pRs->Save(temp_filename, adPersistXML);
		if(FAILED(hr)) return FALSE;
		/* Close the Recordset object and Connection object*/
		pRs->Close();
		pConn->Close();
	}
	catch(_com_error &e)
	{
		dump_error(e);
		return FALSE;
	}
	return TRUE;
}

After you get the XML file, you can transfer it with VC++ Socket to client,

I have introduce it on last post.

In fact, Get XML with SQL and transfer it to the grid of client with

Socket is a popular way in C/S(it includes xml,database,socket,thread… ),

but with the B/S way popularly, this way is fading away.

17,269 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com