錯誤實現該對象可能會導致內存洩漏或下列錯誤之一:
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
%>