how to get return value from procedure sql server 2005 to c#

懵懂的女人 提交于 2019-12-12 04:20:57

问题


CREATE PROCEDURE [dbo].[Code]           
@intEpmName  NUMERIC,            
@strFailedEMPID VARCHAR(1000) output       
AS  

DECLARE    
@FailedCodes VARCHAR(1000)
BEGIN 
----
my  logic where  i need  return the value
SET @strFailedEMPID = @FailedCodes  
----- 
END 

In the stored procedure above, I can send the value as "0" to @strFailedEMPID then to my procedure. However, when I return the value from my procedure, then to the same variable @strFailedEMPID I am sending the value as such:

lsqlParam = new SqlParameter("@strFailedEMPID ", SqlDbType.VarChar);
lsqlParam.Value = "0";
lsqlParam.Direction = ParameterDirection.ReturnValue;
lsqlCmd.Parameters.Add(lsqlParam);

Can anyone help with the correct syntax to get the return value from the procedure?


回答1:


It's because you are defining the parameter in .NET as a ReturnValue which would actually equate to the scenario where you use RETURN within the stored procedure to return an integer (which you're not doing).

Instead, you need to define the @strFailedEMPID parameter as ParameterDirection.Output within your .NET code. If you want to pass a value in AND receive one out through the parameter, use ParameterDirection.InputOutput.

After executing the sproc, you then just:

string value = lsqlCmd.Parameters["@strFailedEMPID"].value;

So....

lsqlParam = new SqlParameter("@strFailedEMPID ", SqlDbType.VarChar);
lsqlParam.Value = "0";
lsqlParam.Direction = ParameterDirection.InputOutput;
lsqlCmd.Parameters.Add(lsqlParam);

lsqlCmd.ExecuteNonQuery();
string value = lsqlCmd.Parameters["@strFailedEMPID"].value;


来源:https://stackoverflow.com/questions/5298822/how-to-get-return-value-from-procedure-sql-server-2005-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!