測試文字功能,小工具,標題

記錄很重要,不然會浪費很多時間在找以前的記憶

一個人的氣度,決定他未來的高度。

2013年7月9日 星期二

Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class M4_linqEnt : System.Web.UI.Page
{
    NorthwindModel.NorthwindEntities northwindEntitie = new NorthwindModel.NorthwindEntities();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            show();
        }
    }

    protected void show()
    {
        var _shippers = from s in northwindEntitie.Shippers
                        select s;
        foreach (var s in _shippers)
        {
            Response.Write(s.ShipperID + " , " + s.CompanyName + " , "+s.Phone + "<br>");
        }
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        NorthwindModel.Shipper _shipper = new NorthwindModel.Shipper();  //新增,要建立一個新的
        _shipper.CompanyName = "123123";
        _shipper.Phone = "0202020202";
        northwindEntitie.Shippers.AddObject(_shipper); //新增add
        northwindEntitie.SaveChanges();
        show();
    }
    protected void btnup_Click(object sender, EventArgs e)
    {
      int txtb1 = int.Parse(TextBox1.Text);
        NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers //修改,要先查詢出資料
                                           where s.ShipperID == txtb1
                                           select s).Single();
        _shipper.CompanyName = "111111";
        _shipper.Phone = "0202-0202";
        northwindEntitie.SaveChanges(); //修改直接存檔
        show();
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        int txtb1 = int.Parse(TextBox1.Text);
        NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers // 刪除也要先查出資料
                                           where s.ShipperID == txtb1
                                           select s).Single();
        northwindEntitie.Shippers.DeleteObject(_shipper); // 刪除DeleteObject
        northwindEntitie.SaveChanges(); //存檔
        show();
    }
}

沒有留言:

張貼留言