需要C#中存储过程的返回值

Chr*_*nce 2 .net c# sql stored-procedures

使用SQL Server 2005中的以下存储过程

IF OBJECT_ID('[dbo].[UserProfile]') IS NOT NULL

DROP PROCEDURE [dbo].[UserProfile]  

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [dbo].[UserProfile] 
(      
      @UserId VARCHAR(20)
)

AS

IF @UserId = 'userId'
BEGIN
SELECT @UserId = 'Yes'
END

ELSE
SELECT @UserId = 'No'

  SELECT @UserId AS RESULT 

  RETURN RESULT

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
Run Code Online (Sandbox Code Playgroud)

C#代码,需要获取存储过程的结果

public String UserProfile(string userId)
{
   String result;
   System.Data.Common.DbCommand cmd = this.mConn.CreateCommand();

   try
   {

       cmd.CommandTimeout = this.mCmdTimeout;
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.CommandText = "UserProfile";

       DbParameter p = cmd.CreateParameter();

       p.ParameterName = "@UserId";
       p.DbType = DbType.String;
       p.Value = userId;

       cmd.Parameters.Add(p);

       cmd.Connection.Open();
       cmd.ExecuteNonQuery();
       cmd.Connection.Close();

       //Need to assign the value of the stored procedure's return to result
       result = Convert.ToString(cmd.Parameters["@UserId"].Value);
   }
   catch (Exception ex)
   {
       throw;
   }
   finally
   {
       cmd.Connection.Close();
   }

   return result;
}
Run Code Online (Sandbox Code Playgroud)

如何在C#中返回存储过程的结果?

use*_*r_v 7

我也在寻找存储过程中的返回值并在C#中捕获它.

我根据我的代码中的逻辑返回了各种返回代码,并且performcalar将无法帮助,因为我想要在适当的代码所需的任何地方之间退出存储过程,而不是使用select.因此,通过添加带有Direction作为ReturnValue的额外参数,您可以捕获返回值.无需OUT参数来捕获存储过程中的返回值.

       preturn.ParameterName = "@Return_Value";  
       preturn.DbType = DbType.Int;        
       preturn.Direction = ParameterDirection.ReturnValue;
Run Code Online (Sandbox Code Playgroud)

所以这就是我想出来的.编辑此帖子中的原始代码以保持简单.

public String UserProfile(string userId)  
{  
   String result;  
   System.Data.Common.DbCommand cmd = this.mConn.CreateCommand();  

   try  
   {  

       cmd.CommandTimeout = this.mCmdTimeout;  
       cmd.CommandType = CommandType.StoredProcedure;  
       cmd.CommandText = "UserProfile";  

       DbParameter p = cmd.CreateParameter();  

       p.ParameterName = "@UserId";  
       p.DbType = DbType.String;  
       p.Value = userId;  

       cmd.Parameters.Add(p);  

       DbParameter preturn = cmd.CreateParameter();        
       preturn.ParameterName = "@Return_Value";  
       preturn.DbType = DbType.Int;        
       preturn.Direction = ParameterDirection.ReturnValue;

       cmd.Parameters.Add(preturn);  

       cmd.Connection.Open();  
       cmd.ExecuteNonQuery();  
       cmd.Connection.Close();  

       //Need to assign the value of the stored procedure's return to result  
       result = Convert.ToString(cmd.Parameters["@Return_Value"].Value);  
   }  
   catch (Exception ex)  
   {  
       throw;  
   }  
   finally  
   {  
       cmd.Connection.Close();  
   }  

   return result;  
}  
Run Code Online (Sandbox Code Playgroud)


Rya*_*ner 6

在这种情况下,您可以使用Command的ExecuteScalar()方法返回值.ExecuteScalar将查看从数据库返回的第一个记录的第一列.