谁能正常运行这个微软的ADO例程???(Save 和 Open 方法范例 )

cachepro 2004-04-21 08:00:09
Save 和 Open 方法范例 (VC++)
这个是ADO使用手册中演示的 Save 和 Open 方法。前两个功能都调试通过,但是最后一个更新却始终无法得到正确的结果,我自己改的程序在跟贴里,下面是原码。盼那位老大能够点拨一下,实在是头大了。再次深表感谢!!!
假设用户在出差时要携带数据库中的一个表。在出发前,用户以 Recordset 访问数据,并将其保存为便携格式。在到达目的地后,用户以本地、断开连接的方式访问 Recordset,在修改 Recordset 后将其保存。最后,当用户回家后,再次连接数据库并用旅途中所做的更改对其进行更新。
这是最后一个函数,是连接数据库并用旅途中所做的更改对其进行更新。

//////////////////////////////////////////////////////////
// //
// 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=srv;"
"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,adLockOptimistic,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;
}
}
}

...全文
29 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cachepro 2004-04-23
hahu(网痞 -- 勿近):
调通是没有问题,关键是UpdateBatch(adAffectAll)没有起作用,不知老大是否更新数据库成功?再次深表感谢!!!
回复
PiggyXP 2004-04-22
太长了,晕,友情up
回复
hahu 2004-04-22
这个代码是来自
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmthsavexvc.asp
然后自己改编的吧
原贴是
把RecordSet保存到一个临时文件里面
然后读取这个临时文件,这时候没有ActiveConnection就可以用
要在Excel,Access两个数据源间转换
RecordSet需要能在两个Connection之间转换的功能
在。Net里面我知道有这样的功能,但是这里就没见过了
我还不知道ADO中的Recordset有这个功能

上面的代码

RstAuthors->Close()以后,数据也没了,应该不能保存到第二个连接里面

回复
hahu 2004-04-22
通了,不知道运行怎么样
Pubs.adtg不知道是什么文件

#import "c:\\program files\\common files\\system\\ado\\msado15.dll" no_namespace rename("EOF", "adoEOF") rename("BOF","adoBOF")
//注意是双斜杠
#include <icrsint.h>
#include <locale.h>
inline void TESTHR(HRESULT x)
{
if FAILED(x)
_com_issue_error(x);
}
#define DATEFMT CString("'%s'")

void CTestAdoDlg::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;
_ConnectionPtr pCnn1 = NULL;

//Definitions of other variables
// _bstr_t strCnn("Provider=sqloledb;Data Source=srv;""Initial Catalog=Pubs;User Id=sa;Password=;");
_bstr_t strCnn("Provider=Microsoft.Jet.OLEDB.4.0;""Data Source=C:\\test\\authors.MDB;Persist Security Info=False");


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

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

//If there is no ActiveConnection, you can open with defaults.
HRESULT hr = S_OK;
hr = pRstAuthors->Open("c:\\test\\Pubs.adtg","Provider=MSPersist;",
adOpenKeyset, adLockOptimistic, adCmdFile);
while( !pRstAuthors->adoEOF ) {
FieldsPtr fptr = pRstAuthors->GetFields();
setlocale(LC_ALL, "chs");
printf("%s %s %s\n", (char *)(_bstr_t)fptr->GetItem(_variant_t((long)0))->GetValue(),
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)1))->GetValue(),
fptr->GetItem(_variant_t((long)2))->GetValue().vt !=VT_NULL ?
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)2))->GetValue() : "NULL");
hr = pRstAuthors->MoveNext();
fptr->Release();
}
hr = pRstAuthors->MoveFirst();
hr = pRstAuthors->Close();

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

pRstAuthors->PutActiveConnection(_variant_t((IDispatch *) pCnn));
hr = pRstAuthors->MoveLast();
hr = pRstAuthors->UpdateBatch(adAffectAll);//关键是这里问题找不到,不能更新
hr = pRstAuthors->Requery(adOptionUnspecified);
hr = pRstAuthors->MoveFirst();
while( !pRstAuthors->adoEOF ) {
//前面定义了rename("BOF","adoBOF"),所以用adoEOF
FieldsPtr fptr = pRstAuthors->GetFields();
setlocale(LC_ALL, "chs");
printf("%s %s %s\n", (char *)(_bstr_t)fptr->GetItem(_variant_t((long)0))->GetValue(),//打印记录
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)1))->GetValue(),
fptr->GetItem(_variant_t((long)2))->GetValue().vt !=VT_NULL ?
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)2))->GetValue() : "NULL");
hr = pRstAuthors->MoveNext();
fptr->Release();
}
hr = pRstAuthors->Close();
// _variant_t vTemp00 ,vTemp01;
//
// hr = pRstAuthors->Open("SELECT * FROM Authors",strCnn,
// adOpenKeyset, adLockBatchOptimistic,adCmdText);
//
// hr = pRstAuthors->Requery(adCmdUnknown);
// vTemp00=pRstAuthors->GetCollect(_variant_t((long)2));

// pRstAuthors->Close();
pCnn->Close();
}
catch(_com_error &e)
{
::AfxMessageBox(_bstr_t(e.Description()));
}
}
回复
tanxin123 2004-04-22
在 stdAfx.h中加入如下代码:
#import "D:\program files\common files\system\ado\msado15.dll"no_namespace rename("EOF", "adoEOF")
#include "icrsint.h"y
inline void TESTHR(HRESULT x)
{if FAILED(x)
_com_issue_error(x);
}
#define DATEFMT CString("'%s'")
有错误:
--------------------Configuration: 前台 - Win32 Debug--------------------
Compiling resources...
H:\前台\stdafx.h(6) : warning RC4011: identifier truncated to 'AFX_STDAFX_H__8921EFBB_738A_452'
H:\前台\stdafx.h(36) : fatal error RC1021: invalid preprocessor command 'import'
Error executing rc.exe.
前台.exe - 1 error(s), 1 warning(s)
请那位高手指点 。
回复
cachepro 2004-04-21
我用ACCESS数据库替换了原文中的SQL数据库,但是就是无法更新,盼老大们指点

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;
_ConnectionPtr pCnn1 = NULL;

//Definitions of other variables
// _bstr_t strCnn("Provider=sqloledb;Data Source=srv;""Initial Catalog=Pubs;User Id=sa;Password=;");
_bstr_t strCnn("Provider=Microsoft.Jet.OLEDB.4.0;""Data Source=C:\\test\\authors.MDB;Persist Security Info=False");


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

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

//If there is no ActiveConnection, you can open with defaults.
HRESULT hr = S_OK;
hr = pRstAuthors->Open("c:\\test\\Pubs.adtg","Provider=MSPersist;",
adOpenKeyset, adLockOptimistic, adCmdFile);
while( !pRstAuthors->EndOfFile ) {
FieldsPtr fptr = pRstAuthors->GetFields();
setlocale(LC_ALL, "chs");
printf("%s %s %s\n", (char *)(_bstr_t)fptr->GetItem(_variant_t((long)0))->GetValue(),
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)1))->GetValue(),
fptr->GetItem(_variant_t((long)2))->GetValue().vt !=VT_NULL ?
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)2))->GetValue() : "NULL");
hr = pRstAuthors->MoveNext();
fptr->Release();
}
hr = pRstAuthors->MoveFirst();
hr = pRstAuthors->Close();

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

pRstAuthors->PutActiveConnection(_variant_t((IDispatch *) pCnn));
hr = pRstAuthors->MoveLast();
hr = pRstAuthors->UpdateBatch(adAffectAll);//关键是这里问题找不到,不能更新
hr = pRstAuthors->Requery(adOptionUnspecified);
hr = pRstAuthors->MoveFirst();
while( !pRstAuthors->EndOfFile ) {
FieldsPtr fptr = pRstAuthors->GetFields();
setlocale(LC_ALL, "chs");
printf("%s %s %s\n", (char *)(_bstr_t)fptr->GetItem(_variant_t((long)0))->GetValue(),//打印记录
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)1))->GetValue(),
fptr->GetItem(_variant_t((long)2))->GetValue().vt !=VT_NULL ?
(char *)(_bstr_t)fptr->GetItem(_variant_t((long)2))->GetValue() : "NULL");
hr = pRstAuthors->MoveNext();
fptr->Release();
}
hr = pRstAuthors->Close();
// _variant_t vTemp00 ,vTemp01;
//
// hr = pRstAuthors->Open("SELECT * FROM Authors",strCnn,
// adOpenKeyset, adLockBatchOptimistic,adCmdText);
//
// hr = pRstAuthors->Requery(adCmdUnknown);
// vTemp00=pRstAuthors->GetCollect(_variant_t((long)2));

// pRstAuthors->Close();
pCnn->Close();
回复
相关推荐
发帖
数据库
创建于2007-09-28

3953

社区成员

VC/MFC 数据库
申请成为版主
帖子事件
创建了帖子
2004-04-21 08:00
社区公告
暂无公告