愛悠閑 > 原創linq對數據庫進行操作(分頁)

原創linq對數據庫進行操作(分頁)

標簽: linq,數據庫,object,string,textbox,system  |  作者: leo_qh 相關  |  發布日期 : 2014-07-12  |  熱度 : 57°

 

近期公司要做新網站,經理讓我用.net3.5 C#3.0 做.

研究了一下linq 本人不善言辭,把今天寫的代碼發出來.

希望和大家一起學習,一起進步.

using  System;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  System.Text;

public   partial   class  _Default : System.Web.UI.Page 
{
    
public string strCn = ConfigurationManager.ConnectionStrings["textConnectionString"].ConnectionString;
    
public TestDataContext Dc;
    
int RecordCount = 0;
    
int PageCount = 1;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Dc 
= new TestDataContext(strCn);
        RecordCount 
= Dc.student.Count();//得到記錄數
        PageCount = (int)Math.Ceiling((decimal)RecordCount / 2); //以2條記錄為一頁,計算總頁碼  
        if (!IsPostBack)
        
{
            ViewState[
"page"= 1;
            Bind();
        }

    }

    
private void Bind()
    
{
        var SkipIndex 
= (int.Parse(ViewState["page"].ToString()) - 1* 2//計算起始索引,以供后面的Skip擴展方法之用
        var result = (from temp in Dc.student select temp).Skip(SkipIndex).Take(2); 
        GridView1.DataSource 
= result;
        GridView1.DataBind();
    }

    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        Bind();
    }

    
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        Bind();
    }

    
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        var temp 
= Dc.student.Single(t => t.id == id);
        Dc.student.DeleteOnSubmit(temp);
        Dc.SubmitChanges();
        Bind();
    }

    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values.ToString());
        
string name = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtname"))).Text;
        var temp 
= Dc.student.Single(t => t.id == id);
        temp.name 
= name;
        Dc.SubmitChanges();
        GridView1.EditIndex 
= -1;
        Bind();
    }

    
protected void btnsave_Click(object sender, EventArgs e)
    
{
        
string name = txtSubName.Text;
        student s 
= new student();
        s.name 
= name;
        Dc.student.InsertOnSubmit(s);
        Dc.SubmitChanges();
        Bind();
        txtSubName.Text 
= "";
    }

    
protected void lbt1_Click(object sender, EventArgs e)
    
{
        LinkButton lb 
= (LinkButton)sender;
        
switch (lb.Text)
        
{
            
case "首頁":
                ViewState[
"page"= 1;
                Bind();
                
break;
            
case "上一頁":
                
if (int.Parse(ViewState["page"].ToString()) > 1)
                
{ ViewState["page"= int.Parse(ViewState["page"].ToString()) - 1; }
                
else
                
{ ViewState["page"= 1; }
                Bind();
                
break;
            
case "下一頁":
                
if (int.Parse(ViewState["page"].ToString()) < PageCount )
                
{ ViewState["page"= int.Parse(ViewState["page"].ToString()) + 1; }
                
else
                
{ ViewState["page"= PageCount ; }
                Bind();
                
break;
            
case "尾頁":
                ViewState[
"page"= PageCount;
                Bind();
                
break;
            
default:
                
break;
        }

    }


}


快乐彩中奖说明