EntityFramework数据持久化查询、增加和删除

sss

 

详情表:

类别表: 

 视图层:

<div>
            <h3 style="text-align:center">添加商品</h3>
            标题:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></br></br>
            发布人:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></br></br>
            内容:<asp:TextBox ID="TextBox3" runat="server" Height="110px" style="margin-bottom: 0px" Width="197px" TextMode="MultiLine"></asp:TextBox></br></br>
            类型:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Value="1">情感</asp:ListItem>
                <asp:ListItem Value="2">小说</asp:ListItem>
                <asp:ListItem Value="3">技术</asp:ListItem>
                <asp:ListItem Value="4">杂谈</asp:ListItem>
                <asp:ListItem Value="5">扯淡</asp:ListItem>
                <asp:ListItem Value="6">夜话</asp:ListItem>
                <asp:ListItem Value="7">C#</asp:ListItem>
            </asp:DropDownList></br></br>
            <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" style="height: 21px" />


            <table border="1">
                <tr>
                    <th>标题</th>
                    <th>发布人</th>
                    <th>内容</th>
                    <th>类型</th>
                    <th>详情</th>
                    <th>操作</th>
                </tr>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <ItemTemplate>
                        <tr>
                            <td><%# Eval("Title")%></td>
                            <td><%# Eval("Author")%></td>
                            <td><%# Eval("Content")%></td>
                            <td><%# Eval("CatelogName")%></td>
                            <td><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm2.aspx">详情</asp:HyperLink></td>
                            <td><asp:LinkButton ID="LinkButton1" runat="server"  CommandArgument='<%# Eval("id")%>' CommandName="delete">删除</asp:LinkButton></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>

代码:

 MyDBEntities db = new MyDBEntities();
//数据库链接
        protected void Page_Load(object sender, EventArgs e)

        {

            var result = from article in db.Article
                         join Catelog in db.Catelog on article.Catelogid equals Catelog.id
                         select new
                         {
                             id = article.id,
                             Title = article.Title,
                             Content = article.Content,
                             Author = article.Author,
                             CatelogName = Catelog.Name
                         };
//连表获取数据集
            Repeater1.DataSource = result.ToList();
            Repeater1.DataBind();
//数据控件绑定
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Article article = new Article();
            article.Title = TextBox1.Text;
            article.Author = TextBox2.Text;
            article.Content = TextBox3.Text;
            article.Catelogid = int.Parse(DropDownList1.SelectedValue);
//绑定控件传值
            db.Article.Add(article);
            int count = db.SaveChanges();
            if (count >= 0)
            {
                Response.Write("<script>alert('添加成功')</script>");
                Page_Load(sender, e);
//刷新界面
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "delete")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                int count = Del(id);
                if (count > 0)
                {
                    Response.Write("<script>alert('删除成功!')</script>");
                    MyDBEntities db = new MyDBEntities();

                }
            }
        }
//方法
        public int Del(int id)
        {
            var FUCK = (from u in db.Article
                        where u.id == id
                        select u).FirstOrDefault();
            db.Article.Remove(FUCK);
            return db.SaveChanges();
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值