一、分页
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BinderToGridView(1,2);
}
}
/// <summary>
/// 通过LinQ技术实现分页数据的提取
/// </summary>
/// <param name="pageIndex">要显示第几页数据</param>
/// <param name="pageSize">每页显示的数据</param>
private void BinderToGridView(int pageIndex,int pageSize)
{
//获取DataContext---获取映射文件---当我们对LinQDB进行对象操作的时候,自动生成相对应的Sql语句--OR
LinQDBDataContext linqDB = new LinQDBDataContext();
//以前我们的分页有2种方式
//数据量大时,我们选择Sql语句分页
//数据量不太大时,我们选择PagedDataSource实现分页
var result= linqDB.User;
//获取总页数
int pageTotal = (result.Count() + pageSize - 1) / pageSize;
this.DropDownList1.DataSource=System.Linq.Enumerable.Range(1, pageTotal).ToList<int>();
this.DropDownList1.DataBind();
//实现分页处理--跳过(pageIndex-1)*pageSize条记录,然后提取pageSize条记录
this.GridView1.DataSource = result.Skip((pageIndex - 1) * pageSize).Take(pageSize);
this.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int pIndex=int.Parse(this.DropDownList1.SelectedValue.ToString());
BinderToGridView(pIndex, 2);
this.DropDownList1.SelectedValue = pIndex.ToString();
}
}
二、增,删,改 实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetRoles();
}
}
private void GetRoles()
{
LinQDBDataContext linqDB = new LinQDBDataContext();
this.rblRoles.DataSource= linqDB.Role.ToList<Role>();//获取所有角色信息并做为单选按钮组的数据源
this.rblRoles.DataTextField = "RoleName";
this.rblRoles.DataValueField = "RoleID";
this.rblRoles.DataBind();
this.rblRoles.Items[0].Selected = true;
}
//增
protected void BtnRegister_Click(object sender, EventArgs e)
{
//判断数据库中是否存在
LinQDBDataContext linqDB = new LinQDBDataContext();
//获取所有用户信息
int i= linqDB.User.Where(p=>p.UserName==this.TxtUsername.Text).Count();
if (i > 0)
{
Response.Write("用户已经存在!");
return;
}
//注册用户信息
//OR映射实现注册
//将要保存的信息封装在对象内部,然后利用OR映射将对象转化为SQL语句
User u = new User();
u.UserName = this.TxtUsername.Text;
u.UserPassword = this.TxtPassword.Text;
u.RoleID = int.Parse(this.rblRoles.SelectedValue);
//在容器中添加一个新对象
linqDB.User.InsertOnSubmit(u);
//更新到数据库
linqDB.SubmitChanges();
}
//改
protected void BtnUpdate_Click(object sender, EventArgs e)
{
//判断数据库中是否存在
LinQDBDataContext linqDB = new LinQDBDataContext();
//用户是否存在
int i = linqDB.User.Where(p => p.UserName == this.TxtUsername.Text).Count();
if (i > 0)
{
User u= linqDB.User.Where(p => p.UserName == this.TxtUsername.Text).Single();
u.UserPassword = this.TxtPassword.Text;
u.RoleID = int.Parse(this.rblRoles.SelectedValue);
//更新数据库
linqDB.SubmitChanges();
}
}
//删
protected void BtnDel_Click(object sender, EventArgs e)
{
//判断数据库中是否存在
LinQDBDataContext linqDB = new LinQDBDataContext();
//用户是否存在
int i = linqDB.User.Where(p => p.UserName == this.TxtUsername.Text).Count();
if (i > 0)
{
//找到该用户
User u = linqDB.User.Where(p => p.UserName == this.TxtUsername.Text).Single();
//删除对象
linqDB.User.DeleteOnSubmit(u);
//更新数据库
linqDB.SubmitChanges();
}
}
}