如何在ado.net中读取存储过程输出值 [英] How to read stored procedure out put value in ado.net

查看:95
本文介绍了如何在ado.net中读取存储过程输出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我写了一个存储过程,返回一个输出值我必须在我的代码中读取该值,这给我一个错误请更正

hi i have wrote a stored procedure which returns one output value i have to read that value in my code which was giving me an error please correct

create proc Authenticate(@Pname varchar(50),@categoryname varchar(55))
as
begin
declare @productcount int
declare @categorycount int
set @productcount=(select count(*) from Product where Pname=@Pname )
set @categorycount=(select count(*)from Category where CategoryName=@categoryname)
if(@productcount!=0  @categorycount!=0)
return 1
else
return 0
end



C#代码下面




C# Code Below

SqlConnection con = new SqlConnection(Constr);
SqlCommand cmd = new SqlCommand("Authenticate",con);
cmd.Parameters.AddWithValue("@Pname", productname);
cmd.Parameters.AddWithValue("@CategoryName", categoryname);
con.Open();
int ctr = (int)cmd.ExecuteScalar();
con.Close();
return ctr;

推荐答案

如果您阅读的代码示例SqlParameterCollection.AddWithValue 此处 [ ^ ]你会看到你' '缺少参数'名称的 @ 前缀。



- Manfred
If you read the code example of SqlParameterCollection.AddWithValue here[^] you''ll see that you''re missing the @ prefix of the parameter''s name.

— Manfred






如果要使用StoredProcedure显示错误消息,则必须创建一个输出参数,然后在代码中调用该参数,请参考以下链接从StoredProcedure获取输出参数到ADO.net





http:// www.c-sharpcorner.com/UploadFile/rohatash/get-out-parameter-from-a-stored-procedure-in-Asp-Net/ [ ^ ]



http:/ /www.aspdotnet-suresh.com/2012/07/get-output-parameter-from-stored.html [ ^ ]


这篇关于如何在ado.net中读取存储过程输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆