怎样将一个XML文件读入到ADO的Recordset中?在线等待,立即给分!!! 我用Recordset打开了某数据库的一张表,并用Recordset的Save方法将Recordset中的内容存成了一个XML文件。那么我用什么方法才能将这个XML文件的内容读入到另外一个Recordset中(客观原因要求我必须从XML文件中读入,而不能直接用Recordset的Clone方法),并将它写入到数据库中? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要用到AppendChunk()和GetChunk()方法,可以参考MSDN中的例子。#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile")#define ChunkSize 100#include <ole2.h>#include <stdio.h>#include "conio.h"#include "malloc.h"#include "AppendChunkX.h"//Function declarationsinline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};void AppendChunkX(void);void PrintProviderError(_ConnectionPtr pConnection);void main(){ if(FAILED(::CoInitialize(NULL))) return; AppendChunkX(); ::CoUninitialize();}void AppendChunkX(void){ _RecordsetPtr pRstPubInfo = NULL; _ConnectionPtr pConnection = NULL; //Define other variables //Interface Pointer declared.(VC++ Extensions) IADORecordBinding *picRs = NULL; CPubInfoRs pubrs; //C++ class object HRESULT hr = S_OK; _bstr_t strCnn("Provider=sqloledb;Data Source=srv;" "Initial Catalog=Pubs;User Id=sa;Password=;"); _bstr_t strMessage,strPubID,strPRInfo; _variant_t varChunk; long lngOffSet,lngLogoSize; char pubId[50]; lngOffSet = 0; UCHAR chData; CHAR ch; SAFEARRAY FAR *psa; SAFEARRAYBOUND rgsabound[1]; rgsabound[0].lLbound = 0; rgsabound[0].cElements = ChunkSize; try { //Open a Connection. TESTHR(pConnection.CreateInstance(__uuidof(Connection))); TESTHR(pConnection->Open(strCnn,"","",NULL)); TESTHR(hr= pRstPubInfo.CreateInstance(__uuidof(Recordset))); pRstPubInfo->CursorType = adOpenKeyset; pRstPubInfo->LockType = adLockOptimistic; TESTHR(pRstPubInfo->Open("pub_info", _variant_t((IDispatch*)pConnection,true), adOpenKeyset,adLockOptimistic,adCmdTable)); //Open an IADORecordBinding interface pointer which we'll use //for Binding Recordset to a class TESTHR(pRstPubInfo->QueryInterface( __uuidof(IADORecordBinding),(LPVOID*)&picRs)); //Bind the Recordset to a C++ Class here TESTHR(picRs->BindToRecordset(&pubrs)); //Display the available logos here strMessage = "Available logos are: " + (_bstr_t)"\n\n"; printf(strMessage); int Counter = 0; while(!(pRstPubInfo->EndOfFile)) { printf("\n%s",pubrs.m_sz_pubid); printf("\n%s",strtok(pubrs.m_sz_prinfo,",")); //Display 5 records at a time and wait for user to continue. if (++Counter >= 5) { Counter = 0; printf("\nPress any key to continue..."); getch(); } pRstPubInfo->MoveNext(); } //Prompt For a Logo to Copy printf("\nEnter the ID of a logo to copy: "); scanf("%s",pubId); strPubID = pubId; //Copy the logo to a variable in chunks pRstPubInfo->Filter = "pub_id = '" + strPubID + "'"; lngLogoSize = pRstPubInfo->Fields->Item["logo"]->ActualSize; //Create a safe array to store the array of BYTES rgsabound[0].cElements = lngLogoSize; psa = SafeArrayCreate(VT_UI1,1,rgsabound); long index1 = 0; while(lngOffSet < lngLogoSize) { varChunk = pRstPubInfo->Fields-> Item["logo"]->GetChunk(ChunkSize); //Copy the data only up to the Actual Size of Field. for(long index=0;index<=(ChunkSize-1);index++) { hr= SafeArrayGetElement(varChunk.parray,&index,&chData); if(SUCCEEDED(hr)) { //Take BYTE by BYTE and advance Memory Location TESTHR(SafeArrayPutElement(psa,&index1,&chData)); index1++; } else break; } lngOffSet = lngOffSet + ChunkSize; } lngOffSet = 0; printf("Enter a new Pub Id: "); scanf("%s",pubrs.m_sz_pubid); strPubID = pubrs.m_sz_pubid; printf("Enter descriptive text: " ); scanf("%c",&ch); gets(pubrs.m_sz_prinfo); pConnection->Execute("INSERT publishers(pub_id) VALUES('" + strPubID + "')",NULL,adCmdText); pRstPubInfo->AddNew(); pRstPubInfo->Fields->GetItem("pub_id")->PutValue(strPubID); pRstPubInfo->Fields->GetItem("pr_info")-> PutValue(pubrs.m_sz_prinfo); //Assign the Safe array to a variant. varChunk.vt = VT_ARRAY|VT_UI1; varChunk.parray = psa; hr = pRstPubInfo->Fields->GetItem("logo")-> AppendChunk(varChunk); pRstPubInfo->Update(); lngLogoSize = pRstPubInfo->Fields->Item["logo"]->ActualSize; printf("New Record : %s\nDescription : %s\nLogo Size : %s\n", pubrs.m_sz_pubid, pubrs.m_sz_prinfo,(LPCSTR)(_bstr_t)pRstPubInfo->Fields-> Item["logo"]->ActualSize); pConnection->Execute("DELETE FROM PUB_INFO WHERE pub_id = '" + strPubID +"'",NULL,adCmdText); pConnection->Execute("DELETE FROM publishers WHERE pub_id = '" + strPubID +"'",NULL,adCmdText); pRstPubInfo->Close(); pConnection->Close(); } catch(_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); PrintProviderError(pConnection); printf("Source : %s \n Description : %s\n", (LPCSTR)bstrSource,(LPCSTR)bstrDescription); }}void PrintProviderError(_ConnectionPtr pConnection){ ErrorPtr pErr = NULL; long nCount = 0; long i = 0; if( (pConnection->Errors->Count) > 0) { nCount = pConnection->Errors->Count; // Collection ranges from 0 to nCount -1. for(i = 0; i < nCount; i++) { pErr = pConnection->Errors->GetItem(i); printf("\t Error number: %x\t%s", pErr->Number, (LPCSTR) pErr->Description); } }}AppendChunkX.h:#include "icrsint.h"class CPubInfoRs : public CADORecordBinding{ BEGIN_ADO_BINDING(CPubInfoRs) ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_sz_pubid, sizeof(m_sz_pubid), l_pubid, TRUE) ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_sz_prinfo, sizeof(m_sz_prinfo), l_prinfo, TRUE) END_ADO_BINDING()public: CHAR m_sz_pubid[10]; ULONG l_pubid; CHAR m_sz_prinfo[200]; ULONG l_prinfo; }; 帮帮忙。。。 用Windows Media Player ActiveX控件的几个问题 socket失效的问题 OPENGL入门求助 如何让一个标签跟随鼠标 请问:为什么有时候不能在其他类调用对话框类 如何把一个控制台程序,转换成mfc程序!! 谁来教我一个关于连接的错误!!!!! 在线等!!! 急!用到ADO安装打包需要哪几个文件??? 如何用C把机器的内存全部测出来 ??????????? 请问,有没有比较好的Defect Tracker一类的工具? 在vc中unsigned short int 和char*如何实现转换!
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")#define ChunkSize 100#include <ole2.h>
#include <stdio.h>
#include "conio.h"
#include "malloc.h"
#include "AppendChunkX.h"//Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void AppendChunkX(void);
void PrintProviderError(_ConnectionPtr pConnection);void main()
{
if(FAILED(::CoInitialize(NULL)))
return; AppendChunkX(); ::CoUninitialize();
}void AppendChunkX(void)
{
_RecordsetPtr pRstPubInfo = NULL;
_ConnectionPtr pConnection = NULL; //Define other variables
//Interface Pointer declared.(VC++ Extensions)
IADORecordBinding *picRs = NULL;
CPubInfoRs pubrs; //C++ class object HRESULT hr = S_OK;
_bstr_t strCnn("Provider=sqloledb;Data Source=srv;"
"Initial Catalog=Pubs;User Id=sa;Password=;"); _bstr_t strMessage,strPubID,strPRInfo;
_variant_t varChunk;
long lngOffSet,lngLogoSize;
char pubId[50];
lngOffSet = 0; UCHAR chData;
CHAR ch;
SAFEARRAY FAR *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = ChunkSize; try
{
//Open a Connection.
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
TESTHR(pConnection->Open(strCnn,"","",NULL)); TESTHR(hr= pRstPubInfo.CreateInstance(__uuidof(Recordset))); pRstPubInfo->CursorType = adOpenKeyset;
pRstPubInfo->LockType = adLockOptimistic; TESTHR(pRstPubInfo->Open("pub_info",
_variant_t((IDispatch*)pConnection,true),
adOpenKeyset,adLockOptimistic,adCmdTable)); //Open an IADORecordBinding interface pointer which we'll use
//for Binding Recordset to a class
TESTHR(pRstPubInfo->QueryInterface(
__uuidof(IADORecordBinding),(LPVOID*)&picRs)); //Bind the Recordset to a C++ Class here
TESTHR(picRs->BindToRecordset(&pubrs)); //Display the available logos here
strMessage = "Available logos are: " + (_bstr_t)"\n\n";
printf(strMessage);
int Counter = 0;
while(!(pRstPubInfo->EndOfFile))
{
printf("\n%s",pubrs.m_sz_pubid);
printf("\n%s",strtok(pubrs.m_sz_prinfo,",")); //Display 5 records at a time and wait for user to continue.
if (++Counter >= 5)
{
Counter = 0;
printf("\nPress any key to continue...");
getch();
}
pRstPubInfo->MoveNext();
} //Prompt For a Logo to Copy
printf("\nEnter the ID of a logo to copy: ");
scanf("%s",pubId);
strPubID = pubId; //Copy the logo to a variable in chunks
pRstPubInfo->Filter = "pub_id = '" + strPubID + "'";
lngLogoSize = pRstPubInfo->Fields->Item["logo"]->ActualSize; //Create a safe array to store the array of BYTES
rgsabound[0].cElements = lngLogoSize;
psa = SafeArrayCreate(VT_UI1,1,rgsabound); long index1 = 0;
while(lngOffSet < lngLogoSize)
{
varChunk = pRstPubInfo->Fields->
Item["logo"]->GetChunk(ChunkSize); //Copy the data only up to the Actual Size of Field.
for(long index=0;index<=(ChunkSize-1);index++)
{
hr= SafeArrayGetElement(varChunk.parray,&index,&chData);
if(SUCCEEDED(hr))
{
//Take BYTE by BYTE and advance Memory Location
TESTHR(SafeArrayPutElement(psa,&index1,&chData));
index1++;
}
else
break;
}
lngOffSet = lngOffSet + ChunkSize;
}
lngOffSet = 0; printf("Enter a new Pub Id: ");
scanf("%s",pubrs.m_sz_pubid);
strPubID = pubrs.m_sz_pubid; printf("Enter descriptive text: " );
scanf("%c",&ch);
gets(pubrs.m_sz_prinfo); pConnection->Execute("INSERT publishers(pub_id) VALUES('" +
strPubID + "')",NULL,adCmdText);
pRstPubInfo->AddNew();
pRstPubInfo->Fields->GetItem("pub_id")->PutValue(strPubID);
pRstPubInfo->Fields->GetItem("pr_info")->
PutValue(pubrs.m_sz_prinfo); //Assign the Safe array to a variant.
varChunk.vt = VT_ARRAY|VT_UI1;
varChunk.parray = psa;
hr = pRstPubInfo->Fields->GetItem("logo")->
AppendChunk(varChunk); pRstPubInfo->Update(); lngLogoSize = pRstPubInfo->Fields->Item["logo"]->ActualSize; printf("New Record : %s\nDescription : %s\nLogo Size : %s\n",
pubrs.m_sz_pubid,
pubrs.m_sz_prinfo,(LPCSTR)(_bstr_t)pRstPubInfo->Fields->
Item["logo"]->ActualSize);
pConnection->Execute("DELETE FROM PUB_INFO WHERE pub_id = '"
+ strPubID +"'",NULL,adCmdText); pConnection->Execute("DELETE FROM publishers WHERE pub_id = '"
+ strPubID +"'",NULL,adCmdText); pRstPubInfo->Close();
pConnection->Close();
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description()); PrintProviderError(pConnection);
printf("Source : %s \n Description : %s\n",
(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}
}void PrintProviderError(_ConnectionPtr pConnection)
{
ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0; if( (pConnection->Errors->Count) > 0)
{
nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("\t Error number: %x\t%s", pErr->Number,
(LPCSTR) pErr->Description);
}
}
}AppendChunkX.h:#include "icrsint.h"class CPubInfoRs : public CADORecordBinding
{
BEGIN_ADO_BINDING(CPubInfoRs) ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_sz_pubid,
sizeof(m_sz_pubid), l_pubid, TRUE) ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_sz_prinfo,
sizeof(m_sz_prinfo), l_prinfo, TRUE) END_ADO_BINDING()public:
CHAR m_sz_pubid[10];
ULONG l_pubid;
CHAR m_sz_prinfo[200];
ULONG l_prinfo; };