从.net发出运行存储过程的问题

时间:2015-06-20 21:35:56

标签: asp.net sql-server visual-studio-2012 stored-procedures sql-server-2008-r2

我有stored procedure

CREATE PROCEDURE [dbo].[GetTransactionFeeType_Pager]
      @PageIndex INT = 1
      ,@PageSize INT = 10
      ,@ProgramName VARCHAR = ''
      ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY ID ASC
      )AS RowNumber
    ,FeeType,TransactionNumber,Amount,TransactionDate,ResponseDescription,PaidUsing

      INTO #Results
      FROM TransactionLog where FeeType=@ProgramName

      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
END

基本上它会返回分页......

现在,从sql server运行并将参数传递为;

时,上面运行正常
1    ,    10   ,     Deposit 

但是使用以下代码:

  string query = "[GetTransactionFeeType_Pager]";
    SqlCommand cmd = new SqlCommand(query);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
    cmd.Parameters.AddWithValue("@PageSize", PageSize);
    cmd.Parameters.AddWithValue("@ProgramName", programID.ToString());
    cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction =                
    ParameterDirection.Output;

与SQL SERVER中的相同; 1,10,存款 但它没有返回任何结果/空结果。我相信它昨天工作正常,所以不知道发生了什么......

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

没有指定长度的

VARCHAR()通常默认长度为1 - 然后默认截断您传递的值。

所以

CREATE PROCEDURE [dbo].[GetTransactionFeeType_Pager]
     @PageIndex INT = 1
     ,@PageSize INT = 10
     ,@ProgramName VARCHAR(99) = ''
     ,@RecordCount INT OUTPUT
AS
...

通过定义@ProgramName

的长度来实现