搞定ADO引入后,我尝试用command方式调用存储过程。存储过程主要是根据输入的参数,执行一个复杂的查询语句,因此返回的是个记录集,同时存储过程本身有个返回参数。程序如下:
{
_CommandPtr cmmd;
_ParameterPtr param;
HRESULT hr = cmmd.CreateInstance(__uuidof(Command));
if(FAILED(hr))
{
AfxMessageBox("GetShipTrack()中创建_CommandPtr对象失败");
return false;
}
cmmd->ActiveConnection = m_piConnection;//需要使用的ADO连接
cmmd->CommandType = adCmdStoredProc;
cmmd->CommandText=_bstr_t("GetShipTrack");
param = cmmd->CreateParameter("",adChar,adParamInput,shipTrackInfo.sMMSIID.GetLength() + 1,_variant_t(shipTrackInfo.sMMSIID));
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adTinyInt, adParamInput, sizeof(BYTE),(BYTE)bShowAIS);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("@ret",adChar, adParamReturnValue,1);
cmmd->Parameters->Append(param);
_Recordset* prec = 0;
HRESULT hr1;
hr1 = cmmd->raw_Execute(NULL, NULL, adCmdStoredProc, &prec);
if(FAILED(hr1))
{
AfxMessageBox("获取船舶航迹信息失败!");
return false;
}
_RecordsetPtr piRecordset;
piRecordset = _RecordsetPtr(prec, false);
问题来了,不管是用raw_Execute还是Execute返回记录集,都不能正确的获取返回参数@ret的值,始终是VT_EMPTY。相信我,存储过程没有错误。肯定给@ret设置了正确的值。
同样的情况,我另一个存储过程是插入一条信息。也有返回值@ret,程序如下:
{
_CommandPtr cmmd;
_ParameterPtr param;
HRESULT hr = cmmd.CreateInstance(__uuidof(Command));
if(FAILED(hr))
{
g_ErrLog.WriteErrorInfo("AddAISDynamicInfo","创建_CommandPtr对象失败");
return false;
}
cmmd->ActiveConnection = m_piConnection;//需要使用的ADO连接
cmmd->CommandType = adCmdStoredProc;
cmmd->CommandText = _bstr_t("AddAISDynamicInfo");
param = cmmd->CreateParameter("",adChar, adParamInput, sMsgData15.sMMSINum.GetLength() + 1,_variant_t(sMsgData15.sMMSINum));
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adTinyInt, adParamInput, sizeof(BYTE), sMsgData15.byNavStatus);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adDouble, adParamInput, sizeof(float),sMsgData15.fSOG);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adDouble, adParamInput, sizeof(float), sMsgData15.fCOG);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adDouble, adParamInput, sizeof(float), (float)sMsgData15.chROT);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adDouble, adParamInput, sizeof(double), sMsgData15.dLong);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adDouble, adParamInput, sizeof(double), sMsgData15.dLat);
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adTinyInt, adParamInput, sizeof(BYTE), (BYTE)(sMsgData15.bPosAcu));
cmmd->Parameters->Append(param);
param = cmmd->CreateParameter("",adSmallInt, adParamInput, sizeof(WORD), (long)sMsgData15.wTrueHeading);
cmmd->Parameters->Append(param);
long nOK = 0;
param = cmmd->CreateParameter("cReturnVal",adBigInt, adParamOutput,sizeof(long),_variant_t(nOK));
cmmd->Parameters->Append(param);
cmmd->Execute(NULL, NULL, adCmdStoredProc);
vreturn=cmmd->Parameters->GetItem("cReturnVal")->GetValue();//通过参数返回值
nOK=(long)vreturn ;
但这时的返回值却是正确的。nOK为1
至今我也没有明白为什么会这样。难道有记录集返回的情况下,就无法获得返回参数了么???