【问题标题】:Using Entity framework calling stored procedure return same value always使用实体框架调用存储过程总是返回相同的值
【发布时间】:2017-05-18 08:55:33
【问题描述】:

我目前正在使用 SQL 服务器、ASP.net Web API 2 和 Entity framework6 技术开发应用程序。

我在下面有一个存储过程:

create procedure [dbo].[get_users_timesheets]

(
    @user_id int,
    @approved_type bit,
    @location_id int,
    @page_size int,
    @page int,
    @count int output
)
as

begin

DECLARE @SQL varchar(1000)

SET @SQL = 'select * from bs_user_timesheets WHERE 

location_id='+STR(@location_id)

IF(@user_id!=-1) SET @SQL = @SQL + ' and user_id='+STR(@user_id)+''

if(@approved_type!='') set @SQL=@SQL+'and approved='+STR(@approved_type)+''

SET @SQL=@SQL+' order by [id] desc'

SET @SQL=@SQL+' OFFSET (('+STR(@page)+'-1)*'+STR(@page_size)+') ROWS'

SET @SQL=@SQL+' FETCH NEXT '+STR(@page_size)+' ROWS ONLY'

exec(@SQL)

SET @count = @@ROWCOUNT

end

我的问题是:一旦我运行以下方法,而不是获取数据库中的实际记录数,我总是在“RetunValue”字段中得到“1”,无论我在那里存储了多少记录。

 object[] parameter = {
        new SqlParameter("@ParametterWithNummvalue", DBNull.Value),
        new SqlParameter("@user_id",user_id),
        new SqlParameter("@approved_type",approved_type),
        new SqlParameter("@location_id",location_id),
        new SqlParameter("@page_size",general.page_size),
        new SqlParameter("@page",page),
        new SqlParameter("@count", SqlDbType.Int) {Direction= ParameterDirection.Output}
        };
        var query = DB.Database.SqlQuery<bs_user_timesheets>("dbo.get_users_timesheets @user_id,@approved_type,@location_id,@page_size,@page,@count output", parameter);
        var result = await query.ToListAsync();
        var ReturnValue = ((SqlParameter)parameter[5]).Value;

enter image description here

如果有人告诉我如何解决这个问题以获得“ReturnValue”字段中的实际记录数,将不胜感激?!

【问题讨论】:

  • 你应该把实际代码放在这里而不是屏幕截图。您正在设置ReturnValue = parameter[5].Value。现在你为什么期望参数之一是返回值?你不觉得这应该是ReturnValue = result.Count吗?
  • 我正在寻找数据库中的记录总数,一次通过输入 :result.count” 我只会根据页面大小限制获得每个页面中的最大记录数
  • 您可以发送 dbo.get_users_timesheets 退货吗?如果发送完整的代码,那么将更容易理解。如果您的过程返回输出,则 ParameterDirection.Output 否则使用 ParameterDirection.ReturnValue
  • 在 c# 中进行计数...只需返回您使这种方式比需要的更复杂的卷。
  • 谢谢。 ParameterDirection.ReturnValue 是对的,我的问题就解决了。

标签: c# sql-server-2012 entity-framework-6


【解决方案1】:

@@Rowcount 将返回受最后一条语句影响的行数。我猜最后一条语句总是影响一条记录。您应该直接执行查询来验证这一点。

【讨论】:

  • 谢谢先生。我更改了@@Rowcount,我的问题就解决了。
猜你喜欢
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多