The following code demonstrates the problem. If the
following code line is uncommented, the code runs without error:
//pStream->Charset = "UTF-8";
Note You may need to change the connection string to reflect your
data source.
Note
You must change uid=<username> and pwd=<strong password> to
the correct values before you run this code. Make sure that uid has the
appropriate permissions to perform this operation on the database.
Sample Code
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <io.h>
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "ADOEOF")
int main()
{
CoInitialize(NULL);
HRESULT hr = S_OK;
_ConnectionPtr pDBConn;
_RecordsetPtr pRS1,pRS2;
_StreamPtr pStream ;
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_bstr_t bstrEmpty(L"");
try
{
struct _finddata_t xml_file;
long hFile;
if( (hFile = _findfirst("c:\\test.xml", &xml_file )) != -1L)
{
DeleteFile("c:\\test.xml");
}
_bstr_t bstrConnect( L"dsn=pubs;uid=<username>;pwd=<strong password>;" );
hr = pDBConn.CreateInstance( __uuidof( Connection ) );
pDBConn->ConnectionString = bstrConnect;
pDBConn->Open( bstrEmpty, bstrEmpty, bstrEmpty,-1 );
hr=pRS1.CreateInstance( __uuidof( Recordset ) );
hr=pRS2.CreateInstance( __uuidof( Recordset ) );
hr=pStream.CreateInstance( __uuidof( Stream ) );
pRS1->Open("Select * from authors",pDBConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRS1->Save("C:\\test.xml",adPersistXML);
//pStream->Charset = "UTF-8";
pStream->Open(vtEmpty,adModeUnknown,adOpenStreamUnspecified,bstrEmpty, bstrEmpty);
pStream->LoadFromFile("C:\\test.xml");
hr = pRS2->Open(pStream.GetInterfacePtr(),vtEmpty,adOpenStatic, adLockOptimistic,adCmdFile);
// check it out
_variant_t RetVal;
RetVal = pRS2->Fields->Item["au_lname"]->Value;
//Cleanup
pRS1->Close();
pRS2->Close();
pDBConn->Close();
}
catch( _com_error &e )
{
// basic error handling
_tprintf(_T("Exception!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
CoUninitialize();
return(0);
}
NOTE: You may notice that if you specify other charsets such as
"iso-8859-1" also works around the problem. For example:
pStream->Charset = "iso-8859-1";
This is because there is no 0xFFFE Unicode byte order marks prepended
to your data when you load the XML file into the ADO Stream object.