Thread View: borland.public.cppbuilder.database.ado
3 messages
3 total messages
Started by Dmitry Konnov
Thu, 06 Jul 2006 10:04
TStreamAdapter, ADO.Recordset
Author: Dmitry Konnov
Date: Thu, 06 Jul 2006 10:04
Date: Thu, 06 Jul 2006 10:04
10 lines
1088 bytes
1088 bytes
Hello, if somebody kows how to read data from TADODataset.Recordset into TStreamAdapter and backward please give me some example code. Thank you. Dmitry
Re: TStreamAdapter, ADO.Recordset
Author: Mark Guerrieri
Date: Fri, 07 Jul 2006 14:08
Date: Fri, 07 Jul 2006 14:08
146 lines
4938 bytes
4938 bytes
> if somebody kows how to read data from
> TADODataset.Recordset into TStreamAdapter and backward
> please give me some example code.
>
Following are excerpts from an inproc COM server DLL I wrote to convert
between Recordsets and XML using streams. You can use the parts that are
relavant to you...
This code assumes you have imported the ADO library, creating the unit
ADODB_TLB.CPP:
#include "AdoDb_TLB.H"
#include <memory>
typedef std::auto_ptr<TStreamAdapter> TStreamAdapterPtr;
typedef std::auto_ptr<TStringStream> TStringStreamPtr;
//---------------------------------------------------------------------------
STDMETHODIMP TRsXmlConvertImpl::RsToXml(LPDISPATCH oRs, BSTR* cXml)
{
HRESULT hResult = S_OK;
IDispatchPtr pDisp(oRs, true);
try
{
*cXml = 0;
if(!pDisp.IsBound())
return E_NOINTERFACE;
StreamPtr pStream = CoStream::Create();
TVariant v;
IUnknownPtr iunk;
OLECHECK(pStream->QueryInterface(IID_IUnknown, (LPVOID*)&iunk));
if(!iunk.IsBound())
throw Exception("Stream interface does not implement the IUnknown
interface");
v = (IUnknown*)iunk;
_RecordsetPtr rs;
OLECHECK(pDisp->QueryInterface(Adodb_tlb::IID__Recordset,
(LPVOID*)&rs));
if(!rs.IsBound())
throw Exception("Object does not implement the IRecordset interface");
OLECHECK(rs->Save(v, adPersistXML));
WideString wStream;
OLECHECK(pStream->ReadText(-1, &wStream));
*cXml = wStream.Detach();
}
catch(Exception& e)
{
hResult = Error(e.Message.c_str(), IID_IRsXmlConvert, E_FAIL);
}
catch(...)
{
hResult = Error(_T("Catastrophic error: internal crash"),
IID_IRsXmlConvert, E_UNEXPECTED);
}
return hResult;
}
//---------------------------------------------------------------------------
STDMETHODIMP TRsXmlConvertImpl::XmlToRs(BSTR cXml, LPDISPATCH* oRs)
{
HRESULT hResult = S_OK;
try
{
if(*oRs != NULL)
(*oRs)->Release();
*oRs = NULL;
if(cXml == 0)
throw Exception("Recordset cannot be created from an empty XML
document");
AnsiString strXml = AnsiString(cXml);
TStringStreamPtr pStream(new TStringStream(AnsiString(cXml)));
if(!pStream.get())
throw Exception("Could not create Stream object");
pStream->Seek(0, soFromBeginning);
TStreamAdapterPtr sa(new TStreamAdapter(pStream.get(), soReference));
if(!sa.get())
throw Exception("Could not create Stream interface");
IUnknownPtr iunk;
OLECHECK(sa->QueryInterface(IID_IUnknown, (LPVOID*)&iunk));
if(!iunk.IsBound())
throw Exception("Stream interface does not implement the IUnknown
interface");
sa.release();
_RecordsetPtr oNewRs;
OLECHECK(CoCreateInstance(CLSID_Recordset, NULL,
CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER,
Adodb_tlb::IID__Recordset, (LPVOID*)&oNewRs));
if(!oNewRs.IsBound())
throw Exception("Could not create the Recordset object- MDAC may not
be installed");
oNewRs->CursorLocation = adUseClient;
oNewRs->LockType = adLockBatchOptimistic;
oNewRs->CursorType = adOpenStatic;
OleVariant eParam;
eParam.VType = varError;
eParam.VError = 0x80020004; //DISP_E_PARAMNOTFOUND
TVariant v = (IUnknown*)iunk;
OLECHECK(oNewRs->Open(v, eParam, -1, -1, -1));
OLECHECK(oNewRs->QueryInterface(IID_IDispatch, (LPVOID*)oRs));
}
catch(Exception& e)
{
hResult = Error(e.Message.c_str(), IID_IRsXmlConvert, E_FAIL);
if(*oRs != NULL)
(*oRs)->Release();
*oRs = NULL;
}
catch(...)
{
hResult = Error(_T("Catastrophic error: internal crash"),
IID_IRsXmlConvert, E_UNEXPECTED);
if(*oRs != NULL)
(*oRs)->Release();
*oRs = NULL;
}
return hResult;
}
//---------------------------------------------------------------------------
Mark
Re: TStreamAdapter, ADO.Recordset
Author: Dmitry Konnov
Date: Mon, 10 Jul 2006 05:43
Date: Mon, 10 Jul 2006 05:43
4 lines
981 bytes
981 bytes
Thank you, Mark this may help.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads