EF框架实现增删改查
EF框架实现增删改查
1.创建数据库:先创建两张表,文章类型表以及文章详情表,设置主外键(ID,Catelogid)
Catelog:文章类型表
字段:Id(自增),Name,[Content]
列名 | 数据类型 |
Id | int |
Name | varchar(50) |
[Content] | text |
Article:文章详情表
字段:Id(自增),Title,Author,[Content],Catelogid
列名 | 数据类型 |
Id | int |
Title | varchar(50) |
Author | varchar(50) |
[Content] | text |
Catelogid | int |
2.创建ASP.NET Web 应用程序(.NET Framework)
创建一个DAO.NET实体数据模型--->来自数据库的EF设计器--->把自己要用的表添加进来(Catelog,Article)
3.给一个web窗体让数据显示出来
实现代码如下:
<form id="form1" runat="server" ><div class="d1"><p style="text-align:center; padding-right:50px;">添加信息</p>标 题:<asp:TextBox ID="txttitle" runat="server"></asp:TextBox><br />作 者 : <asp:TextBox ID="txtauthor" runat="server"></asp:TextBox><br /> 内 容:<asp:TextBox ID="txtcontent" runat="server"></asp:TextBox><br />类 型:<asp:TextBox ID="txtcatelog" runat="server"></asp:TextBox><br />类 型 描 述:<asp:TextBox ID="txtcatecontent" runat="server"></asp:TextBox><br /> <asp:Button ID="Button2" runat="server" Text="添加" OnClick="Button2_Click" Height="27px" Width="89px" CssClass="d3" /><br /> </div><div class="d2"><table border="1"><tr><td style="width:150px">标题</td><td style="width:80px">作者</td><td style="width:150px">内容</td><td style="width:80px">类型名称</td></tr><asp:Repeater ID="Repeater1" runat="server"><ItemTemplate><tr><td><%#Eval("Title") %></td><td><%#Eval("Author") %></td><td><%#Eval("Content") %></td><td><%#Eval("Catelogid") %></td> </tr> </ItemTemplate></asp:Repeater></table></div></form>
我对页面的美观要求比较高,下面是我的页面样式
<style>.d1 {width:350px;height:220px;margin-left:20px;border:1px cadetblue solid;padding-left:50px;background-color:aliceblue}.d2 {margin:auto;text-align:center;margin-top:10px;}.d3 {margin-left:25%;background-color:gainsboro;border:1px solid black;margin-top:10px;}.d2 table {text-align:center;}</style>
在窗体的加载事件中,让想要显示的字段显示出来
用了DAO.NET实体数据模型已经把增删改查的方法封装好了,只需要调用MyDBEntities即可,相对于以前方便了很多
想要页面显示什么字段就查询什么字段,然后绑定到Repeater中
protected void Page_Load(object sender, EventArgs e){MyDBEntities1 db = new MyDBEntities1();var result = from article in db.Articlejoin catelog in db.Catelog on article.Catelogid equals catelog.Idselect new { Title = article.Title, Author = article.Author, Content = article.Content, Catelogid = catelog.Name };this.Repeater1.DataSource = result.ToList();this.Repeater1.DataBind();}
点击添加按钮实现添加以及页面重新加载代码如下:
protected void Button2_Click(object sender, EventArgs e){MyDBEntities1 db = new MyDBEntities1();//同时加两个表Article art = new Article();art.Title = txttitle.Text;art.Content = txtcontent.Text;art.Author = txtauthor.Text;//article.Catelogid = 1;//添加指定的类型//新增类型art.Catelog = new Catelog { Name = txtcatelog.Text, Content = txtcatecontent.Text };//直接调用ef封装好的添加方法db.Article.Add(art);//实时更新到物理数据库db.SaveChanges();int count = db.SaveChanges();if (count >= 0){Response.Write("添加成功");var result = from article in db.Articlejoin catelog in db.Catelog on article.Catelogid equals catelog.Idselect new { Title = article.Title, Author = article.Author, Content = article.Content, Catelogid = catelog.Name };this.Repeater1.DataSource = result.ToList();this.Repeater1.DataBind();}}
可以通过想查询的id 进行查询,代码如下:
protected void Page_Load(object sender, EventArgs e){MyDBEntities1 db = new MyDBEntities1();List<Article> articles = new List<Article>(); //创建一个新的集合对象articles.Add(db.Article.FirstOrDefault(p => p.Id == 4)); //根据你想要查询的id进行查询this.Repeater1.DataSource = articles; //Repeater1绑定值,数据绑定this.Repeater1.DataBind(); //数据显示}
EF框架实现增删改查相关推荐
- 使用EF框架的增删改查和分页的公共类
public class BaseRepository<T> where T : class{//实例化EF框架DataModelContainer db = new DataModelC ...
- java ssm框架做增删改查,使用SSM框架组合实现增删改查的功能
基于ssm框架组合的增删改查功能 ssm框架组合之前已经搭建完成了,下面基于该框架组合实现增删改查的功能,首先创建一个数据库和一张表: CREATE DATABASE `ssmteam` /*!401 ...
- SSM框架——Mybatis增删改查
目录 目录 环境配置 增删改查的实现 查询全部 查询单个ID 添加用户 修改用户 删除用户 增删改查-使用注解开发 思路流程:搭建环境-->导入Mybatis--->编写代码---> ...
- flask框架数据库增删改查
from flask import Flask import os from flask_sqlalchemy import SQLAlchemy import pymysql app=Flask(_ ...
- medoo php 教程,使用Medoo框架完成增删改查功能
摘要:配置Medoo框架: // 配置Medoo框架 // 这是最简单的方法,下载medoo源文件,放到你的PHP开发目录里,载入即可 require 'Medoo/medoo.php'; use M ...
- 使用Mybatis框架完成增删改查操作
一,MyBatis简介 MyBatis是一一个开源.轻量级的数据持久化框架,是JDBC和Hibernate的替代方案. MyBatis 前身为IBatis, 2002 年由Clinton Begin发 ...
- 使用SSM框架实现增删改查
springmvc的理解:https://blog.csdn.net/qq_41879385/article/details/82885516本项目的jsp页面使用bootstrap前端框架:http ...
- 【JAVA线上实习】【SSM框架学习】利用SSM框架完成增删改查-----查
前言 写博客写到脊椎断掉QAQ 知识拓展与延申 -[设计模式] 其实这方面我也不是很懂,有空补充 -[事务] 以上关于事务的所有解释来自于博客: https://blog.csdn.net/CSDN_ ...
- SSM框架的增删改查之用户登录
mapper层 /*** 根据用户名和密码查询*/@Select("select * " +"from user " +"where username ...
最新文章
- 信息系统项目管理师-范围管理知识点
- upplemental Logging
- 8086CPU汇编寻址写法
- 线程魔术技巧:使用Java线程可以做的5件事
- linux 启动/关闭多个py脚本
- mysql state_MySQL进程常见的State【转】
- LeetCode 887. Super Egg Drop
- python excel处理模块_Python(00):openpyxl模块处理Excel文件
- eclipse java环境配置
- 信息系统项目的应急预案方案_【学习】环评、验收、排污许可证、应急预案,都应在项目什么阶段开展?...
- win10计算机用户名修改密码,win10怎么修改administrator账户密码?
- matlab需要多大运存_MATLAB下高效使用内存
- 【二叉树】用python实现AVL树
- thinkadmin中日期的使用
- 小鸡G4工程款 上手体验
- ubuntu22.04装机完配置流程
- 两年计算机考研教训经验贴
- linux编程之emacs
- python3 中英文标点转换
- 一款IM即时通讯聊天系统源码,包含app和后台源码
热门文章
- jQuery封装的表单验证,模仿网易或者腾讯登录的风格
- ASP动态网站建设之连接数据库相关操作
- ​​​​奇迹mu开服务端架设服务器
- 地质地貌卫星影像集锦(三 矿产资源篇)
- 深入探索编译插桩技术(四、ASM 探秘)
- 「游戏」岩浆逃脱2.0
- 2021特种作业操作证电工作业2021电气试验考试题库
- swagger csrf 404_从0岁用到8岁的404页夫人独家超实用中英文双面闪卡素材包
- 云来谌鹏飞:电商的未来在传统企业
- 一个关于在VB.NET中应用超级解霸的问题