简体   繁体   中英

Using Entity framework calling stored procedure return same value always

I'm currently developing an application by using SQL server, ASP.net web API 2 and Entity framework6 technologies.

I have a stored procedure in below :

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

My issue is: Once I run the following method, instead getting the actual number of records in DB, I have all the time got “1” inside the “RetunValue” field regardless of how many records I have stored there.

 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

It would be much appreciated if someone shows me how can I sort this issue out to get the actual number of records inside the “ReturnValue” field?!

@@Rowcount will return the number of rows affected by the last statement. I guess the last statement is always affecting a single record. You should execute the query directly to validate this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM