1. 创建一个简单表及存储过程
create table sp_table
(
username varchar(
30
),
age
int
)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
create procedure test_sp
@username varchar(
30
),
@age
int
,
@out
int
output
as
insert into sp_table values(@username,@age);
select @out
=
count(username) from sp_table
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
declare @a
int
exec test_sp
'
oliver
'
,
21
,@a output;
print @a
2. 创建一个CCommand<CDynamicParameterAccessor,CRowset,CMultipleResults> Rs; 其中第三个参数必须是CMultipleResults,默认为CNoMultipleResults3. 创建存储过程模版
CCommand
<
CDynamicParameterAccessor,CRowset,CMultipleResults
>
Rs;
HRESULT hr
=
Rs.Create(dbconn
->
m_session,
"
exec dbo.QueryStock ?,?,?
"
);
4. 设置参数
hr
=
Rs.Prepare();
void
*
pDummy;
hr
=
Rs.BindParameters(
&
Rs.m_hParameterAccessor,Rs.m_spCommand,
&
pDummy);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
char
*
nParamValue1
=
"
lei
"
;
Rs.SetParam(nParamValue1);
int
nParamValue2
=
22
;
Rs.SetParam((ULONG)
2
,
&
nParamValue2);
5. 调用存储过程
if
(Rs.Open(NULL,NULL,
0
)
!=
S_OK)
...
{
return -1;
}
6. 过滤掉无用的返回结果,找到自己合适的结果集
LONG
*
plRowsAffected
=
NULL;
while
(Rs.GetNextResult(plRowsAffected)
==
S_OK)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if(Rs.m_spRowset != NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Rs.Bind();
while (Rs.MoveNext() == S_OK)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//your code
}
}
}