Dapper分页查询


//api

//数据库连接字符串
string connstr = "Data Source=.;Initial Catalog=Month6;Integrated Security=True";
APIFileHelp help = new APIFileHelp();

//上传文件
[HttpPost]
public FileResult UpLoad()
{
return help.UpLoad();
}

//分页导出
[HttpGet]
public void Export1(int index, int size = 2)
{
SqlConnection conn = new SqlConnection(connstr);
var list = conn.Query<Goods>("select * from Goods").ToList();
list = list.OrderBy(x => x.GId).Skip((index - 1) * size).Take(size).ToList();

Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("GId", "商品编号");
dic.Add("GName", "商品名称");
dic.Add("GColor", "商品颜色");
dic.Add("GSize", "商品尺码");
dic.Add("GPrice", "商品价格");
help.ExportExcel<Goods>("a.xls", list, dic);
}

//显示商品
[HttpGet]
public PageDate GetGoods(int index, int size)
{
SqlConnection conn = new SqlConnection(connstr);
var list = conn.Query<Goods>("select * from Goods").ToList();

PageDate page = new PageDate();
page.List = list.OrderBy(x => x.GId).Skip((index - 1) * size).Take(size).ToList();
var count = list.Count();
page.PageCount = count / size + (count % size == 0 ? 0 : 1);
return page;
}

 

 

 

<a id="a">导出列表</a>

<table class="table table-bordered">
<thead>
<tr>
<td>商品图片</td>
<td>商品名称</td>
<td>商品颜色</td>
<td>商品尺码</td>
<td>商品价格</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tb"></tbody>
</table>

<table class="table table-bordered">
<tr>
<td><input id="Button1" type="button" value="首页" onclick="first()" /></td>
<td><input id="Button1" type="button" value="上一页" onclick="prev()" /></td>
<td><input id="Button1" type="button" value="下一页" onclick="next()" /></td>
<td><input id="Button1" type="button" value="尾页" onclick="last()" /></td>
</tr>
</table>

<script>
var index1 = 0; //获得当前页
var pagecount = 0;
function load(index) {
daoshu();
index1 = index;
$.ajax({
url: "http://localhost:51518/api/Shop/GetGoods2",
data: { index: index, size: 2 },
type: "get",
dataType: "json",
success:
function (d) {
$("#tb").empty();
$(d.List).each(function () {
$("#tb").append(
'<tr>' +
'<td><img src="http://localhost:51518' + this.GImg + '" width="80" height="60" /></td>' +
'<td>' + this.GName + '</td>' +
'<td>' + this.GColor + '</td>' +
'<td>' + this.GSize + '</td>' +
'<td>' + this.GPrice + '</td>' +
'<td><input id="Button1" type="button" value="删除" onclick="del(' + this.GId + ')" />&nbsp;<input id="Button1" type="button" value="修改" onclick="upt(' + this.GId + ')" /></td>' +
'</tr>'
)
})
pagecount = d.PageCount;
}
})
}
load(1);

function first() {
index1 = 1;
daoshu();
load(index1);
}
function prev() {
index1--;
if (index1 == 0) {
index1 = 1;
}
daoshu();
load(index1);
}
function next() {
index1++;
if (index1 > pagecount) {
index1 = pagecount;
}
daoshu();
load(index1);
}
function last() {
daoshu();
load(pagecount);
}

//删除
function del(id) {
var obj = {
GId: id
};
$.ajax({
url: "http://localhost:51518/api/Shop/DeleteGood",
data: obj,
type: "post",
dataType: "json",
success:
function (d) {
if (d > 0) {
alert('删除成功!');
load();
}
else {
alert('删除失败!');
}
}
})
}

//修改
function upt(id) {
location.href = "/Default/Update";
document.cookie = id;
}

//导出
function daoshu() {
$("#a").prop("href", "http://localhost:51518/api/Shop/Export1?index=" + index1);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM