1 using System;
  2  using System.Collections.Generic;
  3  using System.Linq;
  4  using System.Web;
  5  using System.Web.UI;
  6  using System.Web.UI.WebControls;
  7  using System.Configuration;
  8  using System.Data.Linq;
  9  
 10 namespace LinqTest
 11  {
 12      public partial class WebForm1 : System.Web.UI.Page
 13      {
 14          protected void Page_Load(object sender, EventArgs e)
 15          {
 16              if (!IsPostBack)
 17              {
 18                  GetData();
 19              }
 20          }
 21          LinqDataDataContext dc = new LinqDataDataContext(ConfigurationManager.ConnectionStrings["JMDAConnectionString"].ConnectionString);
 22          /// <summary>
 23          /// 使用LINQ查询整张表
 24          /// </summary>
 25          private void GetData()
 26          {
 27              var list = from g in dc.T_GRDA1
 28                         orderby g.Id
 29                         select g;
 30              //使用下面的方法可以实现一个分布的效果 
 31             List<T_GRDA1> ArrayGrda = list.Skip(10 * (9 - 1)).Take(10).ToList();
 32              List<T_GRDA1> ArrayGrda1 = list.ToList();
 33             
 34             this.GridView1.DataSource = ArrayGrda1;
 35              this.GridView1.DataBind();
 36          }
 37          /// <summary>
 38          /// 使用LINQ添加数据
 39          /// </summary>
 40          protected void Button1_Click(object sender, EventArgs e)
 41          {
 42              GetUpdate();
 43              Response.Write("<script>alert('添加成功!')</script>");
 44          }
 45  
 46         private void GetUpdate()
 47          {
 48              T_GRDA1 tr = new T_GRDA1();
 49              tr.Name = txtName.Text;
 50              tr.Mobile = txtMobile.Text;
 51              tr.Address = txtAddress.Text;
 52              tr.Id = txtId.Text;
 53              dc.T_GRDA1.InsertOnSubmit(tr);
 54              dc.SubmitChanges();
 55  
 56         }
 57          /// <summary>
 58          /// 使用LINQ执行查询操作
 59          /// </summary>
 60          /// <param name="sender"></param>
 61          /// <param name="e"></param>
 62          protected void Button4_Click(object sender, EventArgs e)
 63          {
 64              var list = from g in dc.T_GRDA1
 65                         where g.Name.Contains( this.txtName .Text)
 66                         select g;
 67              List<T_GRDA1> ArrayData = list.ToList();
 68              this.GridView1.DataSource = ArrayData;
 69              this.GridView1.DataBind();
 70          }
 71  
 72         protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 73          {
 74              /// <summary>
 75              /// 使用LINQ执行修改
 76              /// </summary>
 77              /// <param name="sender"></param>
 78              /// <param name="e"></param>
 79              if (e.CommandName == "编辑")
 80              {
 81                  T_GRDA1 grda = dc.T_GRDA1.Single(b=>b.Id==e.CommandArgument.ToString());
 82                  this.txtId.Text = grda.Id.ToString();
 83                  this.txtAddress.Text = grda.Address.ToString();
 84                  this.txtMobile.Text = grda.Mobile.ToString();
 85                  this.txtName.Text = grda.Name.ToString();
 86                  GetUpdate();
 87                  this.Response.Write("<script>alert('修改成功!')</script>");
 88              }
 89              /// <summary>
 90              /// 使用LINQ执行删除
 91              /// </summary>
 92              /// <param name="sender"></param>
 93              /// <param name="e"></param>
 94              else if (e.CommandName == "删除")
 95              {
 96                  T_GRDA1 grda = dc.T_GRDA1.Single(b => b.Id == e.CommandArgument.ToString());
 97                  dc.T_GRDA1.DeleteOnSubmit(grda);
 98                  dc.SubmitChanges();
 99                  GetData();
100              }
101          }
102  
103 
104         protected void Button2_Click(object sender, EventArgs e)
105          {
106              var list = from g in dc.T_GRDA1
107                          group g by g.Lay into b
108                          select new T_GRDA1 { Id=b.Key };
109              //foreach (var item in list)
110              //{
111              //    string strId = item.Id;
112              //}
113              List<T_GRDA1> arrayGrda = list.ToList();
114              this.GridView1.DataSource = arrayGrda;
115              this.GridView1.DataBind();
116          }
117  
118         private List<T_GRDA1> getDataList()
119          {
120              var list = from g in dc.T_GRDA1
121                         select g;
122              return list.ToList();
123          }
124  
125     }
126  }

 

posted on 2012-07-05 13:04  杨斐_Feil  阅读(236)  评论(0)    收藏  举报