2017-05-18 21 views
0

我目前正在使用SQL Server,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 @[email protected]+'and approved='+STR(@approved_type)+'' 

SET @[email protected]+' order by [id] desc' 

SET @[email protected]+' OFFSET (('+STR(@page)+'-1)*'+STR(@page_size)+') ROWS' 

SET @[email protected]+' FETCH NEXT '+STR(@page_size)+' ROWS ONLY' 

exec(@SQL) 

SET @count = @@ROWCOUNT 

end 

我的問題是:當我運行下面的方法,而不是獲取的數據庫記錄的實際數量,我把所有的時間得到了「1」內「RetunValue」字段,無論我在那裏存儲了多少條記錄。

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

它將如果有人給我看我怎麼可以排序這個問題了獲得「返回值」字段中記錄的實際數量大加讚賞?

+0

你應該把實際的代碼放在這裏而不是屏幕截圖。 您正在設置'ReturnValue =參數[5] .Value'。現在你爲什麼期望其中一個參數是返回值?你不覺得這應該是'ReturnValue = result.Count'嗎? –

+0

我正在尋找數據庫中的記錄總數,一次放入:result.count「我只會根據頁面大小限制獲得每個頁面中記錄的最大數量 – Developer

+0

您能發送** dbo。 get_users_timesheets **返回?如果發送完整的代碼,那麼會更容易理解。如果你的過程返回輸出然後** ParameterDirection.Output **否則使用** ParameterDirection.ReturnValue ** – SAL

回答

0

@@ Rowcount將返回最後一條語句影響的行數。我想最後一個陳述總是影響單個記錄。您應該直接執行查詢來驗證這一點。

+0

謝謝先生。我更改@@ Rowcount和我的問題解決了。 – Developer

+0

我很高興我的建議解決了您的問題 –

相關問題