如何在VC.net中调用带输出参数的存贮过程.sql?

wxshzf 2003-11-06 10:36:02
我的存贮过程如下:
CREATE PROCEDURE [dbo].[GetCount]
@RstCount int output,
@TblName nvarchar(100)
as
declare @TmpStr nvarchar(1000)
Set @TmpStr=N'select @RstCount=count(*) from '+ @TblName
exec sp_executesql @TmpStr,N'@RstCount int output',@RstCount output
return @RstCount
GO
在查询窗口调用如下
declare @RecCount int
exec GetCount @RecCount output,'yuanqijian'/*元器件表*/
print @RecCount
成功,而我在VC.Net中调用时总出错,代码如下:
int MyCount=0;
CString SqlStr="exec GetCount MyCount output,'yuanqijian'";
m_SqlDB.ExecuteSQL(SqlStr);//m_SqlDB,为数据库对象,已成功连接上数据源
int a=MyCount;
请问SqlStr该如何写?
...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxshzf 2003-11-06
  • 打赏
  • 举报
回复
感谢pengdali(大力 V3.0),请问如果在vc++6.0中该如何调用呢?
pengdali 2003-11-06
  • 打赏
  • 举报
回复
1、
CREATE PROCEDURE [dbo].[GetCount]
@RstCount int output,
@TblName nvarchar(100),
as
declare @TmpStr nvarchar(1000)
Set @TmpStr=N'select @RstCount=count(*) from '+ @TblName
exec sp_executesql @TmpStr,N'@RstCount int output',@RstCount output
GO



2、如:
below is from MSDN,you can find a lot of technical article in MSDN if you search"stored procedure and VC"
good luck

Steps To Reproduce Behavior
In the SQL Server 7.0 Query Analyzer select the test database Pubs.


Create the following stored procedure. This stored procedure returns a recordset and an out parameter count.



if exists (select * from sysobjects where id = object_id(N'[dbo].[GetJobs]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop proc GetJobs
go
create proc GetJobs @id as int, @count as int [out] as
begin
Select @count = Count(*) from jobs where job_id >@id
Select * from jobs where job_id >@id
end
go




Use VC App Wizard to create a new console application and modify the code as follows:



#include "stdafx.h"
#include "stdio.h"
#import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" no_namespace rename ("EOF", "EOF2")

struct InitOle {
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;

int main(int argc, char* argv[])
{
_variant_t varErr((long)0, VT_ERROR);
_CommandPtr comm(__uuidof(Command));
_ConnectionPtr conn(__uuidof(Connection));

_bstr_t connstr="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=(local)";
conn->Open(connstr, "", "", adConnectUnspecified);
comm->ActiveConnection=conn;
comm->CommandText="GetJobs";
comm->CommandType = adCmdStoredProc ;
comm->Parameters->Refresh();
_variant_t recs;

comm->Parameters->Item[_variant_t((short)1)]->Value= _variant_t((long)5);
_RecordsetPtr rs = comm->Execute(&recs, &vtMissing,adCmdStoredProc);

_variant_t recordcount= comm->Parameters->Item[_variant_t((short)2)]->Value;

printf("recordcount = %li\n", (long)recordcount);
return 0;
}



Change the Datasource, User ID and password in the connection string above.




The recordcount variant that the above code returns is of type VT_NULL rather than the number of records that the stored procedure returns.

34,598

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧