Entity_Framework的增删改查,以及绑定GridView和TreeView

来源:互联网 发布:淘宝发货清单表格 编辑:程序博客网 时间:2024/05/20 06:06
EF框架
1.EF的增删改查:
增加:
protected void SaveBtn_Click(object sender, EventArgs e) {

        Model.Entities11 graphsx = new Model.Entities11();
        Model.T_NET_GRAPH_SUB_MK graphEntity=new Model.T_NET_GRAPH_SUB_MK();
        graphEntity.ID = TxtId.Text;   
decimal width;
decimal.TryParse(TxtWidth.Text, out width);
        graphEntity.WIDTH = width;
decimal heigh;
decimal.TryParse(TxtHeigh.Text, out heigh);
        graphEntity.HEIGHT = heigh;
        graphEntity.TITLE = TxtTitle.Text;
        graphEntity.BB = TxtBb.Text;

        graphsx.AddObject("T_NET_GRAPH_SUB_MK", graphEntity);
        graphsx.SaveChanges();
        GrvGraphBind();
}

关于GridView 的编辑和删除
protected void GrvGraph_RowCommand(object sender, GridViewCommandEventArgs e){
        Model.Entities11 graphsx = new Model.Entities11();      
if (e.CommandName == "Editorder"){
string id = e.CommandArgument.ToString();
Model.T_NET_GRAPH_SUB_MK graphEntity = graphsx.T_NET_GRAPH_SUB_MK.First<Model.T_NET_GRAPH_SUB_MK>(u => u.ID == id);
            TxtId.Text = id;
            TxtHeigh.Text = graphEntity.HEIGHT.ToString();
            TxtWidth.Text = graphEntity.WIDTH.ToString();
            TxtTitle.Text = graphEntity.TITLE;
            TxtBb.Text = graphEntity.BB;
        }
else if(e.CommandName == "Delorder") {         

string id = e.CommandArgument.ToString();
            Model.T_NET_GRAPH_SUB_MK graphEntity = graphsx.T_NET_GRAPH_SUB_MK.First<Model.T_NET_GRAPH_SUB_MK>(u => u.ID == id);
            graphsx.DeleteObject(graphEntity);
            graphsx.SaveChanges();
            GrvGraphBind();
        }
}

2.EF的绑定GridView 无条件绑定:
private void GrvGraphBind() {   
var edm = new Model.Entities11();
        GrvGraph.DataSource = edm.T_NET_GRAPH_SUB_MK;
        GrvGraph.DataBind();
    }

联合绑定:
LINQ:
private void GrvLhBind(){
var edm = new Model.Entities11();
var query = from sub in edm.T_NET_SUB_MK
from graph in edm.T_NET_GRAPH_SUB_MK
where sub.ID == graph.SUB_MK_ID
select new{ idmc = sub.TITLE, graph.SQL, graph.TITLE, graph.LABEL, graph.BB, graph.SUB_MK_ID };
        GrvLh.DataSource = query;
        GrvLh.DataBind();
}

根据条件绑定:
ESQL:
ObjectQuery<Model1.T_NET_GRAPH_SUB_MK> query = edm.CreateQuery<Model1.T_NET_GRAPH_SUB_MK>("select value c from T_NET_GRAPH_SUB_MK as c where c.sub_mk_id='ebb3b7ef-3c38-4116-bb11-0c9580cf02e1'");

LINQ:
Var edm= from c in customers  where c.CustomerID == "ALFKI" select c;