如何使用ADO调用带参数的存储过程
编号:QA001372
建立日期: 1999年7月20日 最后修改日期:1999年7月20日
所属类别:
CjrSjj:
vc6
NT4
ADO利用Store Procedure 带参数打开表是未能成功
CComVariant vNULL;
vNULL.vt=VT_ERROR;
vNULL.scode=DISP_E_PARAMNOTFOUND;
_CommandPtr pCommand;
_RecordsetPtr pRs;
_bstr_t CommandName("sp_SelectMachines 'RSLinx OPC Server'");
pCommand->CommandText=CommandName;
pRs->PutRefSouce(pCommand);
try
{
HRESULT hr=pRs->Open(VNULL,vNULL,adOpenForwardOnly,adLockReadOnly,
adCmdStoreProc);
......
执行Open时出现异常, 也无返回码, 似乎根本不存在这个Store Procedure命令,而在 SQL Server Query Analyzer 中,sp_SelectMachines 'RSLinx OPC Server'执行完全正常, 建立另一个Store Procedure命令:
_bstr_t CommandName("sp_SelectMachines1");
用上面的语句,完全正常, 下面是两个Store Procedure命令
CREATE PROCEDURE sp_SelectMachines @aa as VarChar(20)
AS
SELECT DataCollectorName,DataCollectorCode
FROM tblDataCollectors
WHERE OPCServerPID=@aa
ORDER BY DataCollectorCode
...........................
CREATE PROCEDURE sp_SelectMachines1
AS
SELECT DataCollectorName,DataCollectorCode
FROM tblDataCollectors
WHERE OPCServerPID='RSLinx OPC Server'
ORDER BY DataCollectorCode
回答:
以下是我所用的带参数 Store Procedure 过程, 希望对有同样问题者有帮助!
SPname 是Store Procedure 命令, Parameter是所带参数, MySet是所要取得的指针.
HRESULT CVPCollectorView::GetMySet(LPCSTR SPname,CComVariant Parameter,_Recordset **MySet)
{
VARIANT RecordsAffected;
::VariantInit(&RecordsAffected);
RecordsAffected.vt=VT_INT;
VARIANT ap;
::VariantInit(&ap);
Parameter.Detach(&ap);
try
{
pCommand->ActiveConnection=pCon;
pCommand->CommandText=SPname;
pRs=pCommand->Execute(&RecordsAffected,&ap,adCmdStoredProc);
*MySet=pRs.Detach();
}
catch(_com_error *e)
{
CString Error=e->ErrorMessage();
AfxMessageBox(e->ErrorMessage());
}
catch(...)
{
AfxMessageBox("what's going on?");
}
此问题由CjrSjj回答。
| |
|
|
| |
|
|