else set @sql='select top '+str(@pagesize) +' * from ( select top '+str(@recordcount - @pagesize * @pageindex)+' productid,productname, unitprice from products order by productid desc) temptable order by productid asc' exec(@sql)go好了,存储过程建好了,那么如何在.net中使用呢?请看以下代码:
private uint pagecount; //总页数 private uint recordcount; //总记录数
private dataset getpagedata(uint pagesize, uint pageindex)
{
string strconn = system.configuration.configurationsettings.appsettings["connectionstring"];
sqlconnection conn = new sqlconnection(strconn);
conn.open();
sqlcommand command = new sqlcommand("getcustomdatapage",conn); //第一个参数为存储过程名
command.commandtype = commandtype.storedprocedure; //声明命令类型为存储过程
command.parameters.add("@pagesize",sqldbtype.int);
command.parameters["@pagesize"].value = pagesize;
command.parameters.add("@pageindex",sqldbtype.int);
command.parameters["@pageindex"].value = pageindex;
command.parameters.add("@pagecount",sqldbtype.int);
command.parameters["@pagecount"].value = pagecount;
command.parameters["@pagecount"].direction = parameterdirection.output; //存储过程中的输出参数
command.parameters.add("@recordcount",sqldbtype.int);
command.parameters["@recordcount"].value = recordcount;
command.parameters["@recordcount"].direction = parameterdirection.output; //存储过程中的输出参数
sqldataadapter adapter = new sqldataadapter(command);
dataset ds = new dataset();
adapter.fill(ds);
//获得输出参数值
pagecount = convert.touint32(command.parameters["@pagecount"].value);
recordcount = convert.touint32(command.parameters["@recordcount"].value);
conn.close();
return ds;
}
//绑定数据到datagrid中
private void binddatagrid()
{
dataset ds = getpagedata((uint)dgproduct.pagesize,(uint)dgproduct.currentpageindex);
dgproduct.virtualitemcount = (int)recordcount;
dgproduct.datasource = ds;
dgproduct.databind();
}
//页面加载时就绑定datagrid
private void page_load(object sender, system.eventargs e)
{
if(!page.ispostback)
{
binddatagrid();
}
}
//用户翻页时事件处理
private void dgproduct_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
dgproduct.currentpageindex = e.newpageindex;
binddatagrid();
}
//用户单击编辑按纽时事件处理
private void dgproduct_editcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
dgproduct.edititemindex = e.item.itemindex;
binddatagrid();
}
//用户单击取消按纽时事件处理
private void dgproduct_cancelcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
dgproduct.edititemindex = -1;
binddatagrid();
}
//用户单击更新按纽时事件处理
private void dgproduct_updatecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
string strconn = system.configuration.configurationsettings.appsettings["connectionstring"];
sqlconnection conn = new sqlconnection(strconn);
conn.open();
//string strsql = "update from products set [email protected], set [email protected] where [email protected]";
string strsql = "update products set [email protected] where [email protected]";
sqlcommand command = new sqlcommand(strsql,conn);
command.parameters.add("@productname",sqldbtype.nvarchar,40);
command.parameters["@productname"].value = ((textbox)(e.item.cells[1].controls[0])).text.trim();
//command.parameters.add("@unitprice",sqldbtype.int);
//command.parameters["@unitprice"].value = convert.toint32(((textbox)(e.item.cells[2].controls[0])).text.trim());
command.parameters.add("@productid",sqldbtype.int);
command.parameters["@productid"].value = dgproduct.datakeys[e.item.itemindex];
command.executenonquery();
conn.close();
dgproduct.edititemindex = -1;
binddatagrid();
}
//用户单击删除按纽时事件处理
private void dgproduct_deletecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
string strconn = system.configuration.configurationsettings.appsettings["connectionstring"];
sqlconnection conn = new sqlconnection(strconn);
conn.open();
sqlcommand command = new sqlcommand("deleteproduct",conn);
command.commandtype = commandtype.storedprocedure;
command.parameters.add("@productid",sqldbtype.int);
command.parameters["@productid"].value = dgproduct.datakeys[e.item.itemindex];
command.executenonquery();
binddatagrid();
}
//实现删除确认及颜色交替显示功能
private void dgproduct_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
if(e.item.itemtype == listitemtype.item ||e.item.itemtype == listitemtype.alternatingitem)
{
button btndelete = (button)(e.item.cells[4].controls[0]);
btndelete.attributes.add("onclick","javascript:return confirm('确定删除?')");
e.item.attributes.add("onmouseover","this.style.backgroundcolor='#ffcc66'");
e.item.attributes.add("onmouseout","this.style.backgroundcolor='#ffffff'");
} }
新闻热点
疑难解答