EF框架实现增删改查

       EF框架实现增删改查

1.创建数据库:先创建两张表,文章类型表以及文章详情表,设置主外键(ID,Catelogid)

Catelog:文章类型表

字段:Id(自增),Name,[Content]

列名数据类型
Idint
Namevarchar(50)
[Content]text


 

Article:文章详情表

字段:Id(自增),Title,Author,[Content],Catelogid

列名数据类型
Idint
Titlevarchar(50)
Authorvarchar(50)
[Content]text
Catelogidint

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>
            标   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 题:<asp:TextBox  ID="txttitle" runat="server"></asp:TextBox><br />
            作   &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 者 :&nbsp; <asp:TextBox  ID="txtauthor" runat="server"></asp:TextBox><br />             
            内   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 容:<asp:TextBox  ID="txtcontent" runat="server"></asp:TextBox><br />
            类   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 型:<asp:TextBox  ID="txtcatelog" runat="server"></asp:TextBox><br />
            类 型 描 述:<asp:TextBox  ID="txtcatecontent" runat="server"></asp:TextBox><br />           
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;           
            <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.Article
                         join catelog in db.Catelog on article.Catelogid equals catelog.Id
                         select 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.Article
                             join catelog in db.Catelog on article.Catelogid equals catelog.Id
                             select 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(); //数据显示
        }

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值