




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一、首先创建两张表,产品表T_Product和T_ProClass在这里,我们把两张表的关系,主外键设置好二、现在我们通过vs2012添加Entity Framework选择ADO.NET实体数据模型,添加选择从数据库生成连接成功单击完成后,会生成如图所示的实体模型我们可以看到在项目中生成了两个文件Model.edmx和Model.Designer.cs这两个文件分别用来描述实体以及他们之间关系,和自动生成的C#代码,包括实体类的代码以及他们之间的代码三、框架已经搭建好,现在我们就可以通过linq语法实现对数据的查询,修改,删除,增加等操作了首先,我们来查询一下产品的列表在页面中使用Repeat控件用来显示数据 产品ID 产品名?称? 产品价?格? 产品类别e 代码很简单,就是一个简单的查询public partial class Index : System.Web.UI.Page EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if(!IsPostBack) ProBind(); public void ProBind() var proList = from s in ef.T_Product select s; Repeater1.DataSource = proList; Repeater1.DataBind(); 在这里我们要重点说一下,因为数据库的主外键关系已经被映射到对象中,所以我们可以通过对象直接调用和它关联的对象,在这里就是可以通过产品T_Product直接调用类别T_ProClass好,现在我们在来看看如何添加产品在页面中添加文本框和按钮 产品名?称? 产品价?格? 产品类别e 后台代码public partial class Insert : System.Web.UI.Page EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if(!IsPostBack) bindProClass(); protected void bindProClass() var query = from ps in ef.T_ProClass select ps; RadioButtonList1.DataSource = query; RadioButtonList1.DataTextField = proClassName; RadioButtonList1.DataValueField = ID; RadioButtonList1.DataBind(); protected void Button1_Click(object sender, EventArgs e) T_Product product = new T_Product(); product.ProClass =Convert.ToInt32(RadioButtonList1.SelectedValue); product.ProName = txtProName.Text; product.ProPrice = Convert.ToDecimal(txtProPrice.Text); ef.T_Product.AddObject(product); ef.SaveChanges(); ef.Dispose(); Response.Write(alert(添加成功|);location.href=Index.aspx); 修改和添加很类似页面代码: 产品名?称? 产品价?格? 产品类别e 后台的代码 EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) int id = Convert.ToInt32(Request.QueryStringproId.ToString(); bindProClass(); ProdectById(id); protected void bindProClass() var query = from ps in ef.T_ProClass select ps; RadioButtonList1.DataSource = query; RadioButtonList1.DataTextField = proClassName; RadioButtonList1.DataValueField = ID; RadioButtonList1.DataBind(); protected void ProdectById(int id) var product = (from p in ef.T_Product where p.ID = id select p).Single(); RadioButtonList1.SelectedValue = product.ProClass.ToString(); txtProName.Text = product.ProName; txtProPrice.Text = product.ProPrice.ToString(); protected void Button1_Click(object sender, EventArgs e) int id = Convert.ToInt32(Request.QueryStringproId.ToString(); var product = (from p in ef.T_Product where p.ID = id select p).Single(); product.ProClass = Convert.ToInt32(RadioButtonList1.SelectedValue); product.ProName = txtProName.Text; product.ProPrice = Convert.ToDecimal(txtProPrice.Text); ef.SaveChanges(); ef.Dispose(); Response.Write(alert(修T改?成功|);location.href=Index.aspx); 删除的代码 EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) int id = Convert.ToInt32(Request.QueryStringproId.ToString(); var product = (from p in ef.T_Product where p.ID = id s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 劳动教育课程评价体系的构建与实践
- 农业机械化生产技术及应用题解
- 企业考勤管理表格
- 2025年中考英语主题复习(日常生活 自我管理)课件
- 2025年浙江省高中自主招生考试科学试卷试题(含答案详解)
- 危险化学品-经营安全管理制度与岗位操作流程
- 顾客心理在新零售中的角色与策略
- 音乐节庆典活动的视觉设计时尚设计的色彩与构图实践
- 风光摄影的视觉冲击力探讨
- 颠覆性科技创业项目路演
- 《2025年CSCO肾癌诊疗指南》解读
- 《财务课件:特殊资产处理详解》
- 化工行业典型事故案例分析
- 网络舆情分析模型-全面剖析
- 课题申报书:生成式人工智能赋能高校体育教师教学能力的内在机理与实践路径研究
- 防水材料管理制度
- 军品管理制度
- 中职生课堂纪律管理
- 抗菌药物处方专项点评制度
- 2025年人教版中考英语复习核心词汇1600词+英汉【2022新课标】
- dq加盟合同范例
评论
0/150
提交评论