XML文件更新到数据库

YimTangwen 2005-05-30 09:09:48
我现要做数据备份,已经将Access表成功保存为XML文件;想用下面的代码将XML文件更新到数据库;调试没有错误,可是表里面的数据不能更新。问题在哪??
///将记录集保存为XML文件
pRecordset->Save((_variant_t)fullpathname, adPersistXML);
....
///打开XML文件
hr = pRecordset->Open((_variant_t)fullpathname,_variant_t((IDispatch*pConnection),adOpenForwardOnly,adLockOptimistic,adCmdFile);
pRecordset->PutActiveConnection(_variant_t((IDispatch*)pConnection));
///将XML文件更新到数据库
hr = pRecordset->UpdateBatch(adAffectAll);
pRecordset->Close();
救救我啊!!
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lincolnfz 2005-08-29
  • 打赏
  • 举报
回复
hr = pRecordset->Open((_variant_t)fullpathname,_variant_t((IDispatch*pConnection),adOpenForwardOnly,adLockOptimistic,adCmdFile);

adLockOptimistic 改为adLockBatchOptimistic 试试

我是用数据流,这样做可以,不懂你行不,good luck~~~
zhouaiju 2005-08-29
  • 打赏
  • 举报
回复
你是不是只把adLockOptimistic 改为adLockBatchOptimistic 就可以了,还是用读文件方式把XML数据读出来,然后再更新到数据库中呢?
zhouaiju 2005-08-29
  • 打赏
  • 举报
回复
真的很谢谢你,lincoInfz兄弟。
zhouaiju 2005-08-27
  • 打赏
  • 举报
回复
那位问这个问题的兄弟,不知道你现在调通了吗?我现在遇到和你一样的问题。如果你解决了,一定要发上结贴哟!求了!
YimTangwen 2005-05-31
  • 打赏
  • 举报
回复
大哥,我就是按照书里的这例子做的,不行啊;我运行光盘带的这个程序,也一样的不能更新数据。
你帮忙调调。谢谢!!
kingzai 2005-05-30
  • 打赏
  • 举报
回复
//////////////////////////////////////////////////////////
// //
// SaveX3 Function //
// //
//////////////////////////////////////////////////////////
//Finally, you return home. Now update the database with
//your changes.
void SaveX3()
{
HRESULT hr = S_OK;

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstAuthors = NULL;
_ConnectionPtr pCnn = NULL;

//Definitions of other variables
_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
"Initial Catalog=pubs;User Id=sa;Password=;");

try
{
TESTHR(pCnn.CreateInstance(__uuidof(Connection)));

TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));

//If there is no ActiveConnection, you can open with defaults.
pRstAuthors->Open("a:\\pubs.adtg","Provider=MSPersist;",
adOpenForwardOnly,adLockBatchOptimistic,adCmdFile);

//Connect to the database, associate the Recordset with
//the connection, then update the database table with the
//changed Recordset.
pCnn->Open(strCnn,"","",NULL);

pRstAuthors->PutActiveConnection(_variant_t((IDispatch *) pCnn));
pRstAuthors->UpdateBatch(adAffectAll);

pRstAuthors->Close();
pCnn->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstAuthors->GetActiveConnection();

// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}

///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;

if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;

// Collection ranges from 0 to nCount -1.
for(long i = 0;i < nCount;i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number,
(LPCSTR) pErr->Description);
}
}
}

///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////

void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());

// Print COM errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}

bool FileExists()
{
struct _finddata_t xml_file;
long hFile;

if( (hFile = _findfirst("a:\\pubs.xml", &xml_file )) != -1L)
{
printf( "File already exists!\n" );
return(true);
}
else
return (false);
}
// EndSaveCpp
kingzai 2005-05-30
  • 打赏
  • 举报
回复
Save and Open Methods Example (VC++)
These three examples demonstrate how the Save and Open methods can be used together.

Assume you are going on a business trip and want to take along a table from a database. Before you go, you access the data as a Recordset and save it in a transportable form. When you arrive at your destination, you access the Recordset as a local, disconnected Recordset. You make changes to the Recordset, then save it again. Finally, when you return home, you connect to the database again and update it with the changes you made on the road.

// BeginSaveCpp
#import "c:\Program Files\Common Files\system\ado\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

#include <ole2.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>

//Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
bool FileExists(void);
void SaveX1(void);
void SaveX2(void);
void SaveX3(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

//////////////////////////////////////////////////////////
// //
// Main Function //
// //
//////////////////////////////////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;

//If File exists in the specified directory, then display error
if (!FileExists())
{
SaveX1();
SaveX2();
SaveX3();
}

::CoUninitialize();
}

//////////////////////////////////////////////////////////
// //
// SaveX1 Function //
// //
//////////////////////////////////////////////////////////

//First, access and save the authors table.
void SaveX1()
{
HRESULT hr = S_OK;

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstAuthors = NULL;

//Definitions of other variables
_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
"Initial Catalog=pubs;User Id=sa;Password=;");

try
{
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));

pRstAuthors->Open("SELECT * FROM authors",strCnn,
adOpenDynamic,adLockBatchOptimistic,adCmdText);

// For sake of illustration, save the Recordset to a diskette
// in XML format.
pRstAuthors->Save("a:\\pubs.xml",adPersistXML);

pRstAuthors->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstAuthors->GetActiveConnection();

// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}

//////////////////////////////////////////////////////////
// //
// SaveX2 Function //
// //
//////////////////////////////////////////////////////////
//At this point, you have arrived at your destination.
//You will access the authors table as a local, disconnected Recordset.
//Don't forget you must have the MSPersist provider on the machine you
//are using in order to access the saved file, a:\pubs.xml.
void SaveX2()
{
HRESULT hr = S_OK;

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstAuthors = NULL;

try
{
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));

//For sake of illustration, we specify all parameters.
pRstAuthors->Open("a:\\pubs.xml","Provider=MSPersist;",
adOpenForwardOnly,adLockBatchOptimistic,adCmdFile);

//Now you have a local, disconnected Recordset.
//Edit it as you desire.
//(In this example, the change makes no difference).
pRstAuthors->Find("au_lname = 'Carson'",NULL,adSearchForward);
if (pRstAuthors->EndOfFile)
{
printf("Name not found ...\n");
pRstAuthors->Close();
return;
}
pRstAuthors->GetFields()->GetItem("City")->PutValue("Chicago");
pRstAuthors->Update();

// Save changes in ADTG format this time, purely for sake of
// illustration. Note that the previous version is still on the
// diskette as a:\pubs.xml.
pRstAuthors->Save("a:\\pubs.adtg",adPersistADTG);

pRstAuthors->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstAuthors->GetActiveConnection();

// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧