是不是第一種方法還缺點什麽,爲什麽我在調試是一到p->dosomething() 就報錯。
解决方案 »
- 新手求教::如何使slider控件的滑块立刻移动到鼠标点击的位置
- 求Designing ActiveX Components with the MFC Document/View Model正确的连接网址或相关资料,例子和代码
- modem检测对方按键信息的问题!
- 启动应用程序
- <求助>关于Visual Assist?
- 如何得到父窗口对话框类的成员变量?
- GetMenuItemInfo()函数的使用,急用。
- 智能指针
- 用VC远程访问oracle,有时(不是每次)会出现ORA-01756的错误?
- 终极目标->消息流程图
- 惊异发现:块注释最多只能注释400行
- 关于CEdit 输出如何分行显示数据???
用了智能指针
就是说,你不用去管应用计数了
还有,你有没有忘了CoInitialize?
ADO 2.7 Samples See Also
Resync Method© 1998-2001 Microsoft Corporation. All rights reserved.
Resync Method Example (VC++)
This example demonstrates using the Resync method to refresh data in a static recordset.// BeginResyncCpp
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")#include <ole2.h>
#include <stdio.h>
#include <conio.h>// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ResyncX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);/////////////////////////////
// //
// Main Function //
// //
/////////////////////////////void main()
{
if(FAILED(::CoInitialize(NULL)))
return; ResyncX(); ::CoUninitialize();
}/////////////////////////////////
// //
// ResyncX Function //
// //
/////////////////////////////////void ResyncX(void)
{
HRESULT hr = S_OK; // Define string variables.
_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
"Initial Catalog=pubs;User Id=sa;Password=;"); // Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstTitles = NULL; try
{
// Open recordset for titles table.
TESTHR(pRstTitles.CreateInstance(__uuidof(Recordset)));
pRstTitles->CursorLocation = adUseClient;
pRstTitles->CursorType = adOpenStatic;
pRstTitles->LockType = adLockBatchOptimistic;
pRstTitles->Open ("titles",strCnn,
adOpenStatic, adLockBatchOptimistic, adCmdTable); // Change the type of the first title in the recordset.
pRstTitles->Fields->GetItem("type")->Value =
(_bstr_t) ("database"); // Display the results of the change.
printf("\nBefore resync: \n\n"); printf("Title - %s\n\n",(LPSTR) (_bstr_t) pRstTitles->
Fields->GetItem("title")->Value); printf("Type - %s\n\n",(LPSTR) (_bstr_t) pRstTitles->
Fields->GetItem("type")->Value); // Resync with database.
pRstTitles->Resync(adAffectAll,adResyncAllValues); // Display the results of the resynch.
printf("\n\nAfter resync: \n\n"); printf("Title - %s\n\n",(LPSTR) (_bstr_t) pRstTitles->
Fields->GetItem("title")->Value); printf("Type - %s\n\n",(LPSTR) (_bstr_t) pRstTitles->
Fields->GetItem("type")->Value); pRstTitles->CancelBatch(adAffectAll);
pRstTitles->Close();
} catch (_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstTitles->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("\t 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);
}
// EndResyncCppSee Also
Resync Method© 1998-2001 Microsoft Corporation. All rights reserved.