保存ADO的记录集为XML文件

#include "stdafx.h"
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
int main(int argc,char* argv[])
{
printf("Export recordset as xml file\n");
CoInitialize(NULL);
try
{
_variant_tvarOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
_ConnectionPtr pConn("ADODB.Connection");
pConn->Open(_bstr_t("Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=(local)"),"","",adConnectUnspecified);
_StreamPtrstream;
stream.CreateInstance("ADODB.Stream");
stream->raw_Open(varOptional,adModeUnknown,adOpenStreamUnspecified,NULL,NULL);
stream->put_Type(adTypeText);
_CommandPtr Cmd1;
Cmd1.CreateInstance(__uuidof(Command));
Cmd1->ActiveConnection = pConn;
Cmd1->CommandText= _bstr_t("Select * from Table1 for XML Auto");
PropertiesPtr props;
props=Cmd1->GetProperties();
PropertyPtrprop;
prop = props->Item[_variant_t("XML Root")];
prop->PutValue(_variant_t("root"));
prop = props->Item[_variant_t("Output Stream")];
prop->PutValue(_variant_t((IDispatch *)stream, true));
Cmd1->Execute(&varOptional,&varOptional,adExecuteStream);
stream->SaveToFile("test.xml",adSaveCreateOverWrite);
printf("Save stream to file finished\n");
stream->Close();
pConn->Close();
}
catch(_com_error &e)
{
char buf[1024];
wsprintf(buf,"Description = '%s'\n", (char*)e.Description());
::MessageBox(NULL,buf,"error",MB_OK);
}
::CoUninitialize();
return 0;
}

你可能感兴趣的:(C++,c,xml,Security,C#)