C#EntityFramework 对SQL数据库进行增删改查操作

使用EntityFramework访问数据库可以减少以前搭建三层架构书写代码的工作量,减轻开发的时间。

1.数据表的设计

2.创建项目以及使用EF数据模型

前面两个操作完成接下来就是代码部分

前端代码:

<body>

    <form id="form1" runat="server">

            <div style="display:flex;">

                <asp:HiddenField ID="HiddenField1" runat="server" />

                <table border="1">

                <tr>

                    <th>标题</th>

                    <th>作者</th>

                    <th>内容</th>

                    <th>类型</th>

                    <th>操作</th>

                </tr>

            <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">

                <ItemTemplate>

                    <tr>

                    <th><%# Eval("title") %></th>

                    <th><%# Eval("author") %></th>

                    <th><%# Eval("content") %></th>

                    <th><%# Eval("catrlogid") %></th>

                    <th>

                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="del" CommandArgument='<%# Eval("title") %>'>删除</asp:LinkButton>

                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="update" CommandArgument='<%# Eval("id") %>'>修改</asp:LinkButton>

                    </th>

                </tr>



                </ItemTemplate>

            </asp:Repeater>

            </table>



        <div style="width:500px;height:500px;display:flex;margin-left:5%;">

        <div>

            <asp:Label ID="Label1" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

            <asp:Label ID="Label2" runat="server" Text="作者"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />

            <asp:Label ID="Label3" runat="server" Text="内容"></asp:Label><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> 

            <asp:Label ID="Label4" runat="server" Text="类型"></asp:Label><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />

            <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />

          </div>

        <div style="margin-left:5%;">

            <asp:Label ID="Label5" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />

            <asp:Label ID="Label6" runat="server" Text="作者"></asp:Label><asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />

            <asp:Label ID="Label7" runat="server" Text="内容"></asp:Label><asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br /> 

            <asp:Label ID="Label8" runat="server" Text="类型"></asp:Label><asp:DropDownList ID="DropDownList2" runat="server" ></asp:DropDownList><br />

            <asp:Button ID="Button2" runat="server" Text="修改" OnClick="Button2_Click"/>

          </div>

          </div>



        </div>

    </form>

</body>

前端代码生成界面:

后端代码:

//Web窗体类后面的所有代码

MyDBEntities2 db = new MyDBEntities2();//引用实体模型,这里是连接建好以后筛选数据下面文本框得命名 数据库名+Entities(第一个),后面第二个就是Entities2 以此类推

// 是注释

        protected void Page_Load(object sender, EventArgs e)

        {

            //回发

            if (!IsPostBack)

            {

                Data();

                DropData();

            }

        }

        //数据显示

        public void Data()

        {

            //LINQ查询数据语句

            var data = from c in db.Catelog

                      join a in db.Article on c.id equals a.catrlogid

                      select new

                      {

                          id = a.id,

                          title = a.title,

                          author = a.author,

                          content = a.content,

                          catrlogid = c.name

                      };

            Repeater1.DataSource = data.ToList();//绑定数据源,ToList()把数据转换为 list集合

            Repeater1.DataBind();//显示绑定的数据

        }

        //下拉框数据

        public void DropData()

        {

            var dataDrop = from c in db.Catelog

                          select new

                          {

                              id = c.id,

                              name = c.name

                          }

                      ;

            DropDownList1.DataSource = dataDrop.ToList();//绑定数据源,ToList()把数据转换为 list集合

            DropDownList1.DataTextField = "name";//列表项可见部分文本

            DropDownList1.DataValueField = "id";//列表项的值部分

            DropDownList1.DataBind();//显示绑定的数据

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "")

            {

                Response.Write("<script>alert('请输入完整')</script>");

                return;

            }

            else

            {

                var data = from a in db.Article

                          where a.title == TextBox1.Text

                          select a.title;

                foreach (var item in data)

                {

                    if (item == TextBox1.Text)

                    {

                        Response.Write("<script>alert('已存在,请输入其他标题')</script>");

                        return;

                    }

                    else

                    {

                        break;

                    }

                }

                int cid = int.Parse(DropDownList1.SelectedValue);//获取到选定项的值  DropDownList1.DataValueField = "id";列表项的值部分

                Article ar = new Article(TextBox1.Text, TextBox2.Text, TextBox3.Text, cid);//创建对象保存需要添加到数据库的值

                db.Article.Add(ar);

                if (db.SaveChanges() > 0)

                {

                    Response.Write("<script>alert('添加成功')</script>");

                    TextBox1.Text = "";//清空文本框

                    TextBox2.Text = "";//清空文本框

                    TextBox3.Text = "";//清空文本框

                    Data();//调用数据显示方法重新显示

                    DropData();//调用下拉框数据方法

                }

            }

        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)

        {

            if (e.CommandName == "del")

            {

                string title = e.CommandArgument.ToString();//获取删除所需标题,ID也可以

                db.Article.Remove(db.Article.FirstOrDefault(n => n.title == title));

                if (db.SaveChanges() > 0)

                {

                    Response.Write("<script>alert('删除成功')</script>");

                    Data();

                    DropData();

                }

            }

            if (e.CommandName == "update")

            {

                aid = int.Parse(e.CommandArgument.ToString());//获取修改所需ID

                HiddenField1.Value = aid.ToString();//给隐藏控件的赋值

                var data = from a in db.Article

                          where a.id == aid

                          select a;

                foreach (var item in data.ToList())

                {

                    TextBox4.Text = item.title;

                    TextBox5.Text = item.author;

                    TextBox6.Text = item.content;

                    int id = (int)item.catrlogid;

                    //DropDownList1.Text = item.catrlogid;

                    DropData(id);

                    DropDownList2.Items[id - 1].Selected = true;//选中传过来的下拉框类型名

                }

            }

        }

        public int aid;//全局变量接受修改作者ID

        public void DropData(int id)

        {

            var dataDrop = from c in db.Catelog

                          select new

                          {

                              newid = c.id,

                              name = c.name

                          };

            ;

            DropDownList2.DataSource = dataDrop.ToList();

            DropDownList2.DataTextField = "name";

            DropDownList2.DataValueField = "newid";

            DropDownList2.DataBind();

        }

        protected void Button2_Click(object sender, EventArgs e)

        {

            if (TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "")

            {

                Response.Write("<script>alert('请输入完整')</script>");

                return;

            }

            else

            {

                    if (HiddenField1.Value == "")

                    {

                        Response.Write("<script>alert('没找到修改的数据源')</script>");

                        return;

                    }

                    else

                    {

                    int hidden = int.Parse(HiddenField1.Value);

                    var updateData = db.Article.FirstOrDefault(n => n.id == hidden);

                        updateData.title = TextBox4.Text;

                        updateData.author = TextBox5.Text;

                        updateData.content = TextBox6.Text;

                        updateData.catrlogid = int.Parse(DropDownList2.SelectedValue);

                        int count = db.SaveChanges();//入库返回值

                        if (count > 0)

                        {

                            Response.Write("<script>alert('修改成功')</script>");

                            TextBox4.Text = "";

                            TextBox5.Text = "";

                            TextBox6.Text = "";

                            Data();

                        }

                    }

            }

        }

后端功能:

所有数据显示

删除

删除后的数据

添加和修改的输入完整

添加标题相同提示

主要是查重

 

没点修改会找不到用户

 

第一次写博客,没经验,欢迎留言讨论。

联系QQ:2976258702  备明来意

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值