kenlistian

厚积薄发. 勤为桨,思为帆

   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理 ::
  73 随笔 :: 4 文章 :: 22 评论 :: 0 Trackbacks
使用_commandPtr 处理存储过程时,当在存储过程中需要返回结果集合和返回参数时,一般在任何语言调用的ado中
存储过程控件都无法同时返回,这里http://www.pkvs.com/data/mssql/19323.html给了我们答案。

使用CreateParameter(),而是调用CommandPtr的Refresh()函数先从数据库中查询参数.
_ConnectionPtr m_pConn;
m_pConn.CreateInstance(__uuidof(Connection));
m_pConn->Open("driver={SQL Server};server=127.0.0.1;DATABASE=pub;UID=sa;PWD=", "","",0);

_CommandPtr  m_pCommand;
m_pCommand.CreateInstance(__uuidof(Command));
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));

m_pCommand->ActiveConnection = m_pConn;
m_pCommand->CommandText = "SP_XX";              //存储过程名
m_pCommand->PutCommandType(adCmdStoredProc);
m_pCommand->Parameters->Refresh();              //从数据库查询参数信息
//我不建议使用refresh,这个内部的处理速度还不如用createparamter好,

long cnt = m_pCommand->Parameters->GetCount();//取得参数的个数
for(long k=1;k<cnt;k++)
{ //由于ADO中认为返回值是第一个参数,因此这里用k=1滤掉第一个参数
       m_pCommand->Parameters->GetItem(k)->Value = XXX;//按存储过程的参数顺序给参数赋值
}

以执行这个存储过程了,返回m_pRecordset将返回集合,
    m_pRecordset = m_pCommand->Execute(0,0,adCmdStoredProc);
这个时候,如果接下来用
   _variant_t ret_val = m_pCommand->Parameters->GetItem((long)0)->Value;
那么将得不到值
而如果像下面这样调用的话就可以得到返回值了
m_pRecordset->Close();
_variant_t output_para = m_pCommand->Parameters->GetItem((long)0)->Value;
MS ADO.net给这一现象的回复是:
 You can think of a stored procedure as a function in your code. The function doesn’t return a value until it has executed all of its code. If the stored procedure returns results and you haven’t finished processing these results, the stored procedure hasn’t really finished executing. Until you’ve closed the DataReader, the return and output parameters of your Command won’t contain the values returned by your stored procedure.
也就是说Execute()函数应该看成是直到m_pRecordset关掉以后才会正确返回.


posted on 2007-09-18 10:17 kenlistian 阅读(572) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理