代码之家  ›  专栏  ›  技术社区  ›  Chris Ballance

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

  •  2
  • Chris Ballance  · 技术社区  · 16 年前

    在SQLServer2005中使用以下存储过程

    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
    

    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;
    }
    

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

    6 回复  |  直到 16 年前
        1
  •  7
  •   user_v    12 年前

    我还从存储过程中寻找返回值,并在C#中捕获它。

    我根据代码中的逻辑返回了各种返回代码,executescalar不会有帮助,因为我希望在需要时使用适当的代码从存储过程中返回,而不使用select。 因此,通过添加一个额外的参数,将Direction作为ReturnValue,可以捕获返回值。不需要OUT参数来捕获存储过程的返回值。

           preturn.ParameterName = "@Return_Value";  
           preturn.DbType = DbType.Int;        
           preturn.Direction = ParameterDirection.ReturnValue;
    

    这就是我想到的。在本文中编辑原始代码以保持简单。

    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;  
    }  
    
        2
  •  6
  •   Ryan Brunner    16 年前

        3
  •  4
  •   AJ.    16 年前

    在执行命令之前,请尝试将其添加到参数的属性中:

    cmd.Parameters["@UserId"].Direction = System.Data.ParameterDirection.InputOutput;
    


    正如Ryan Brunner在注释中所述,您必须在存储过程中将参数标记为OUT,这样才能工作。

        4
  •  3
  •   m3kh    16 年前

    存储过程中:

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

    代码:

    p.ParameterName = "@UserId";
    p.DbType = DbType.String;
    p.Direction = System.Data.ParameterDirection.Output;
    p.Value = userId;
    
        5
  •  1
  •   Sinister Beard    11 年前

        6
  •  -1
  •   Russell Steen    16 年前

    使用。。。?

    林肯?将存储过程拖到设计器中,它将成为c#中的一个函数,返回正确的值。 Linq2Sql?将存储过程拖到设计器中,它将成为c#中的一个函数,返回正确的值。 ADO?-只需将ExecuteScalar的结果分配给一个变量