谁能正常运行这个微软的ADO例程???(Save 和 Open 方法范例 )
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;
}
}
}