(selectm" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

sql server 帶輸入輸出參數(shù)的分頁存儲過程(效

系統(tǒng) 2166 0

create procedure proc_page_withtopmax
(
@pageIndex int,--頁索引
@pageSize int,--每頁顯示數(shù)
@pageCount int output,--總頁數(shù),輸出參數(shù)
@totalCount int output--總條數(shù)
)
as
begin
set nocount on;

declare @sql nvarchar(1000)
set @sql='select top 10 * from tb_testtable where (id> (select max(id) from (select top '+str((@pageIndex-1)*@pageSize)+' id from tb_testtable order by id) as temp)) order by id'
execute(@sql)

declare @sqlRecordCount nvarchar(1000) --得到總記錄條數(shù)的語句
set @sqlRecordCount=N'select @recordCount=count(*) from tb_testtable'
declare @recordCount int --保存總記錄條數(shù)的變量
exec sp_executesql @sqlRecordCount,N'@recordCount int output',@recordCount output

if( @recordCount % @pageSize = 0) --如果總記錄條數(shù)可以被頁大小整除
set @pageCount = @recordCount / @pageSize --總頁數(shù)就等于總記錄條數(shù)除以頁大小
else --如果總記錄條數(shù)不能被頁大小整除
set @pageCount = @recordCount / @pageSize + 1 --總頁數(shù)就等于總記錄條數(shù)除以頁大小加1

set @totalCount = @recordCount

set nocount off;
end

--數(shù)據(jù)庫中執(zhí)行該存儲過程
declare @pageCount int, @totalCount int

exec proc_page_withtopmax 2,95955,@pageCount output,@totalCount output

select '總頁數(shù):'+str(@pageCount)
select '總條數(shù):'+str(@totalCount)

?

C# 代碼調(diào)用該帶輸入輸出參數(shù)的分頁存儲過程

?

public static DataSet GetRecordByPage( int pageSize, int pageIndex, out int pageCount, out int totalCount)
{
DataSet ds = new DataSet();
try
{
using (SqlConnection conn = new SqlConnection(@"server=;database=data_test;uid=; pwd=;"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int));
SqlParameter param = new SqlParameter("@totalCount", SqlDbType.Int);
param.Direction = ParameterDirection.Output;
cmd.Parameters.Add(param);

SqlParameter param1 = new SqlParameter("@pageCount", SqlDbType.Int);
param1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(param1);

cmd.Parameters[0].Value = pageSize;
cmd.Parameters[1].Value = pageIndex;
cmd.Parameters[2].Value = 0;
cmd.Parameters[3].Value = 0;

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "proc_page_withtopmax";
cmd.CommandTimeout = 180;

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;

DataSet source = new DataSet();
adapter.Fill(ds);

object o = cmd.Parameters["@totalCount"].Value;
totalCount = (o == null || o == DBNull.Value) ? 0 : System.Convert.ToInt32(o);

object b = cmd.Parameters["@pageCount"].Value;
pageCount = (b == null || o == DBNull.Value) ? 0 : System.Convert.ToInt32(b);
}
}
catch (SqlException e)
{
throw e;
}
return ds;
}

}

sql server 帶輸入輸出參數(shù)的分頁存儲過程(效率最高)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 微山县| 宁城县| 庆安县| 巴林右旗| 莱阳市| 思南县| 玉门市| 北流市| 贵溪市| 德州市| 灵寿县| 松滋市| 徐水县| 丹棱县| 沛县| 潢川县| 灵寿县| 隆化县| 苍南县| 怀集县| 金昌市| 云林县| 廊坊市| 和田县| 磴口县| 辉南县| 抚顺县| 铁岭县| 德惠市| 汽车| 太谷县| 西畴县| 进贤县| 无为县| 墨江| 鹤山市| 张家口市| 蓬溪县| 宜黄县| 荔波县| 海城市|