请大家帮忙看看这段ADO程序,多谢!

oceanwhh 2003-11-14 04:38:32
在ADO中使用COMMAND来执行一个带有输出参数的存储过程,在数据库中执行有输出参数值,但是ADO中的输出参数为0,代码如下:
_ConnectionPtr pConn;
_CommandPtr pComm;
_RecordsetPtr pRs;

pConn.CreateInstance("ADODB.Connection");
pComm.CreateInstance("ADODB.Command");
pRs.CreateInstance("ADODB.Recordset");

//connect DB;
pConn->Open("DATABASE","sa","",adModeUnknown);

//exe sql;
pComm->ActiveConnection=pConn;
pComm->CommandText="MYPROC";
pComm->CommandType=adCmdStoredProc;

_ParameterPtr outParam=pComm->CreateParameter(_bstr_t ("maxid"),adInteger,adParamOutput,sizeof(int));
pComm->Parameters->Append(outParam);

pRs=pComm->Execute(NULL,NULL,adCmdStoredProc);

long out=pComm->Parameters->Item[(short)0]->Value;
CString str;
str.Format("maxid is %d",out);
AfxMessageBox(str);

存储过程如下:
CREATE procedure MYPROC
@maxid int output
as
select @maxid=( max(id)) from temp;

...全文
65 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-11-14
  • 打赏
  • 举报
回复
试试:
_ConnectionPtr pConn;
_CommandPtr pComm;
_RecordsetPtr pRs;

pConn.CreateInstance("ADODB.Connection");
pComm.CreateInstance("ADODB.Command");
pRs.CreateInstance("ADODB.Recordset");

//connect DB;
pConn->Open("DATABASE","sa","",adModeUnknown);

//exe sql;
pComm->ActiveConnection=pConn;
pComm->CommandText="MYPROC";
pComm->CommandType=adCmdStoredProc;

pComm->Parameters->Refresh

pRs=pComm->Execute(NULL,NULL,adCmdStoredProc);

long out=pComm->Parameters->Item["@maxid"]->Value;
CString str;
str.Format("maxid is %d",out);
AfxMessageBox(str);
pengdali 2003-11-14
  • 打赏
  • 举报
回复
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.

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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