WEB开发网
开发学院软件开发VC 如何实现返回记录集的 Visual C++ COM 对象 阅读

如何实现返回记录集的 Visual C++ COM 对象

 2008-11-12 19:27:31 来源:WEB开发网   
核心提示:错误实现该对象可能会导致内存泄漏或下列错误之一: The operation requested by the application is not allowed if the object is closed. - 或 - 类型不匹配 - 或 - error 'ASP 0115' - A trapp

错误实现该对象可能会导致内存泄漏或下列错误之一:

The operation requested by the application is not allowed if the object is closed.

- 或 -

类型不匹配

- 或 -

error 'ASP 0115' - A trappable error occured in an external object

更多信息

使用下列步骤实现一种方法,该方法从 Visual C++ COM 对象向 Active Server Pages 返回记录集。1.创建一个名为“PassRs”的 ATL DLL 项目。2.插入一个名为“PassRsObj”的 ATL 对象。 3.添加包含以下信息的方法:Method Name: TestMethod

Parameters : [out, retval] LPDISPATCH* ppRecordset

4.在对象的实现文件中包括下面一行:#import "msado15.dll" no_namespace rename("EOF", "adoEOF")

5.按如下所示实现该方法:

注意:您必须先将 UID=<username> 和 pwd=<strong password> 更改为正确的值,然后才能运行此代码。请确保该 UID 具有在数据库中执行此操作所需的适当权限。

STDMETHODIMP CPassRsObj::TestMethod(LPDISPATCH *ppRecordset )
{
  _ConnectionPtr pConn;
  _RecordsetPtr pRs;
  
  pConn.CreateInstance(__uuidof(Connection));
  pRs.CreateInstance(__uuidof(Recordset));
  pConn->Open("DSN=pubs;uid=<username>;pwd=<strong password>;", (BSTR) NULL, (BSTR) NULL, -1);
  //Client side cursor is required for disconnected recordsets
  pRs->CursorLocation = adUseClient;
  pRs->Open( "select * from authors",
        pConn.GetInterfacePtr(),
        adOpenKeyset, adLockOptimistic, -1);
  // Disconnect the recordset
  pRs->PutRefActiveConnection(NULL);
  //Clone the recordset.
  //NOTE: Recordset to be cloned must support bookmarks
  pRs->Clone(adLockOptimistic)->QueryInterface(IID_IDispatch, (void**) ppRecordset);
  pRs->Close();
  pConn->Close();
  pRs = NULL;
  pConn = NULL;  
  return S_OK;
}
          6.创建含有以下代码的 ASP 页:<%
Dim rsTest, oTestPassRs
Set oTestPassRs = Server.CreateObject("PassRs.PassRsObj")
Set rsTest = oTestPassRs.TestMethod()
Do
  Response.Write ( "Value in Record = " & rsTest(1) & "<BR>" )
  rsTest.MoveNext
Loop until rsTest.EOF
rsTest.Close
Set rsTest = Nothing
Set oTestPassRs = Nothing
%>

Tags:如何 实现 返回

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接