存储过程中的输出参数,并使用sqlparameter对象在c#中检索它 [英] output parameter in stored procedure and retrieving it in c# with sqlparameter object

查看:64
本文介绍了存储过程中的输出参数,并使用sqlparameter对象在c#中检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个存储过程,通过它我已经传递了三个输出参数。

如何在c#中的windows应用程序中检索这些输出参数值?



我试图通过 sqlparameter 对象检索它但是生成错误

'参数@return不包含参数对象'。



所以请任何人告诉我怎样才能从

SqlParameter object或 SqlParameterCollection object。





I have written a stored procedure through which I have passed three output parameters.
How can I retrieve these output parameter values in windows application in c#?

I tried to retrieve it through sqlparameter object but there is an error generated
'the parameter @return does not contain in parameter object'.

So please any one tell me how can I retrieve the three output parameter form c# with
SqlParameter object or SqlParameterCollection object.


Alter procedure str_com
(
@id int,
@comid int output,
@brid int output
)
as

begin
select @comid=cid,@brid=bid from t1 where id=@id
end





如何检索 @comid @brid 在c#中带有 SqlParameter 对象或 SqlParameterCollection 对象。



谢谢



how can I retrieve @comid and @brid in c# with SqlParameter object or SqlParameterCollection Object.

thanks

推荐答案

我发现此链接非常有用。



http://stackoverflow.com/questions / 706361 / getting-return-value-from-stored-procedure-in-c-sharp [ ^ ]
I found this link very helpful.

http://stackoverflow.com/questions/706361/getting-return-value-from-stored-procedure-in-c-sharp[^]


CREATE PROCEDURE [dbo].[GetFruitName]
      @FruitId INT,
      @FruitName VARCHAR(30) OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
     
      SELECT @FruitName = FruitName
      FROM Fruits
      WHERE FruitId = @FruitId
END







using (SqlCommand cmd = new SqlCommand("GetFruitName", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@FruitId", int.Parse(txtFruitId.Text.Trim()));
            cmd.Parameters.Add("@FruitName", SqlDbType.VarChar, 30);
            cmd.Parameters["@FruitName"].Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            TextBox1.Text =cmd.Parameters["@FruitName"].Value.ToString();
        }


这篇关于存储过程中的输出参数,并使用sqlparameter对象在c#中检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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