EntiyFranmework数据持久化的增删查改

设计数据库

          创建数据库MyDB,创建两个表,判断表是否有关联,创建关联。

            articel表                                                                                    catelong表

          EntiyFranmework数据持久化的增删查改

添加数据模型到EF设计器

EntiyFranmework数据持久化的增删查改

      设置静态页面

        静态页面样式

EntiyFranmework数据持久化的增删查改

静态页面代码

完成前端静态页面后实现增删查改功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    //在加载事件中完成repeat控件显示查询,并且绑定下拉框类型查询
    public partial class WebForm1 : System.Web.UI.Page
    {
        //调用数据模型的MyDBEntities1类
        MyDBEntities1 db = new MyDBEntities1();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询article模型类,绑定数据源到repeat
                this.Repeater1.DataSource = db.article.ToList();
                this.Repeater1.DataBind();
                //查询catelong模型类,绑定到下拉框
                DropDownList1.DataSource = db.catelong.ToList();
                DropDownList1.DataValueField = "id";
                DropDownList1.DataTextField = "name";
                DropDownList1.DataBind();
                //加载时把更新与取消更新隐藏
                Button2.Visible = false;
                Button3.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //判断文本框的值是否为空,不为空则执行添加否则显示数据填充不完整
            if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "")
            {
                //点击添加按钮时把文本框和下拉框的值添加到数据库里面去
                article article = new article();
                article.title = TextBox1.Text;
                article.content = TextBox3.Text;
                article.author = TextBox2.Text;
                article.catelogid = int.Parse(DropDownList1.SelectedValue);
                db.article.Add(article);
                //判断是否添加成功,数据库返回的受影响行数是否大于0
                if (db.SaveChanges() > 0)
                {
                    Response.Write("<script>alert('添加成功')</script>");
                    this.Repeater1.DataSource = db.article.ToList();
                    this.Repeater1.DataBind();
                }
            }
            else
            {
                Response.Write("<script>alert('数据填充不完整')</script>");
            }
        }

        protected void Repeater1_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {
            //获取需要执行操作哪一行的Id
            int id = int.Parse(e.CommandArgument.ToString());
            //当CommandName时del时进行删除操作
            if (e.CommandName == "del")
            {
                //根据Id删除行数据
                db.article.Remove(db.article.FirstOrDefault(a => a.Id == id));
                if (db.SaveChanges() > 0)
                {
                    Response.Write("<script>alert('删除成功')</script>");
                    Repeater1.DataSource = db.article.ToList();
                    Repeater1.DataBind();
                }
            }
            //当CommandName时update时进行更改操作
            else if (e.CommandName == "update")
            {
                //根据id把数据绑定到文本框与下拉框
                var arl = db.article.FirstOrDefault(a => a.Id == id);
                HiddenField1.Value = arl.Id.ToString();
                TextBox1.Text = arl.title;
                TextBox3.Text = arl.content;
                TextBox2.Text = arl.author;
                DropDownList1.Text = arl.catelong.id.ToString();
                //点击更新时隐藏添加按钮,显示更新与取消更新按钮
                Button1.Visible = false;
                Button2.Visible = true;
                Button3.Visible = true;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //点击取消更新时,文本框为空,添加按钮显示,更新与取消更新隐藏
            TextBox1.Text = "";
            TextBox3.Text = "";
            TextBox2.Text = "";
            Button1.Visible = true;
            Button2.Visible = false;
            Button3.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //获取HiddenField1上的Id值
            int id = int.Parse(HiddenField1.Value);
            //根据id更改行数据
            var s = db.article.FirstOrDefault(a => a.Id == id);
            s.title = TextBox1.Text;
            s.content = TextBox3.Text;
            s.author = TextBox2.Text;
            s.catelong.name = DropDownList1.SelectedValue;
            //判断受影响行数是否大于0,大于0则更新成功
            if (db.SaveChanges() > 0)
            {
                Response.Write("<script>alert('更新成功')</script>");
                //重新刷新数据
                this.Repeater1.DataSource = db.article.ToList();
                this.Repeater1.DataBind();
                //更新成功后文本框为空,添加按钮显示,更新与取消更新按钮隐藏
                TextBox1.Text = "";
                TextBox3.Text = "";
                TextBox2.Text = "";
                Button1.Visible = true;
                Button2.Visible = false;
                Button3.Visible = false;
            }
        }
    }
}

 

 

上一篇:vue+element 通过checkbox控制el-table-column的显示或隐藏


下一篇:HDU 4737 A Bit Fun (2013成都网络赛)