PDA

View Full Version : سوال: دریافت مقدار پارامتر برگشتی



sece_shance
یک شنبه 03 خرداد 1388, 20:00 عصر
سلام؛


مخواستم تعدادی پست را با شرایط خاصی بصورت صفحه بندی شده دریافت کنم و در نهایت تعداد تمامی پست ها را در یک پارامتر بازگشتی دریافت کنم که بصورت زیر عمل کردم:


Sql:






ALTER PROCEDURE [dbo].[Posts_GeyByCategoryIDPaged]
@CategoryID int,
@CurrentPage int,
@PageSize int,
@DateTime DateTime,
@TotalRecord int output
AS
BEGIN

DECLARE @FirstRow int, @LastRow int;
SET @FirstRow = (@PageSize + 1) * (@CurrentPage - 1);
SET @LastRow = (@PageSize * @CurrentPage);

WITH [TEMP] as (

SELECT [Posts].*, ROW_NUMBER() OVER (ORDER BY [PublishDate] DESC) as [RowNumber]
FROM [Posts] INNER JOIN [PostsCategories]
ON [Posts].[PostID] = [PostsCategories].[PostID]
WHERE [Active] = 1 AND [Posts].[PublishDate] <= @DateTime AND [PostsCategories].[CategoryID] = @CategoryID

)

SELECT * FROM [TEMP] WHERE [RowNumber] BETWEEN @FirstRow AND @LastRow
ORDER BY [PublishDate] DESC, [PostID] Desc

SELECT @TotalRecord = Count(*) FROM [TEMP]

END
RETURN


Server Side:







DateTime dateTime = DateTime.Now;
bool LoadComplate = false;
this.CreatePostStatusByPostMode(mode, ref dateTime, ref LoadComplate);

SqlConnection connection = new SqlConnection(this.ConnectionString);
SqlCommand commond = new SqlCommand("Posts_GeyByCategoryIDPaged", connection);
commond.CommandType = CommandType.StoredProcedure;

commond.Parameters.Add(new SqlParameter("@CategoryID", SqlDbType.Int));
commond.Parameters.Add(new SqlParameter("@CurrentPage", SqlDbType.Int));
commond.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));
commond.Parameters.Add(new SqlParameter("@DateTime", SqlDbType.DateTime));
commond.Parameters.Add(new SqlParameter("@TotalRecord", SqlDbType.Int));

commond.Parameters[0].Value = CategoryID;
commond.Parameters[1].Value = CurrentPage;
commond.Parameters[2].Value = PageSize;
commond.Parameters[3].Value = dateTime;
commond.Parameters[4].Direction = ParameterDirection.Output;

IDataReader reader = null;

try
{
connection.Open();
reader = this.ExecuteReader(commond);

if (reader.FieldCount < 1)
return null;

List<Post> posts = this.CTypeReaderToCollectionDTO(reader, LoadComplate);
TotalRecord = Convert.ToInt32(commond.Parameters[4].Value);

return posts;

}
catch { throw; }
finally
{
if (reader != null)
{
reader.Close();
}
if (connection.State != ConnectionState.Closed)
{
connection.Close();
commond.Dispose();
}
}


تمامی موارد با موفقیت دریافت میشن جز تعداد تمامی پست ها! نمدونم مشکل از کجاست.
ممنون میشم راهنماییم کنید.