当前位置:   article > 正文

EF数据持久化(三层架构,客户增删)

EF数据持久化(三层架构,客户增删)

效果图 

 

 点击新增按钮

 

点击添加 

 

添加成功展示新增数据 

 

点击删除,出现删除选项,点击确定根据id删除成功 

 

成功删除 

 

 实现过程

 Model设置具体流程在下面链接中

 https://blog.csdn.net/Mr_wangzu/article/details/136805824?spm=1001.2014.3001.5501

 DAL

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using WebApplication1.Models;
  6. namespace WebApplication1.DAL
  7. {
  8. public class CustomersDBService
  9. {
  10. public static List<Customer> Show(){
  11. CustomersDBEntities db = new CustomersDBEntities();
  12. return db.Customers.ToList();
  13. }
  14. public static bool Add(Customer cu) {
  15. CustomersDBEntities db = new CustomersDBEntities();
  16. db.Customers.Add(cu);
  17. return db.SaveChanges()>0;
  18. }
  19. public static bool shan(int id) {
  20. CustomersDBEntities db = new CustomersDBEntities();
  21. Customer model = new Customer();
  22. model.CID = id;
  23. db.Entry(model).State = System.Data.EntityState.Deleted;
  24. db.Customers.Remove(model);
  25. return db.SaveChanges()>0;
  26. }
  27. }
  28. }

 BLL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using WebApplication1.Models;
  6. namespace WebApplication1.BLL
  7. {
  8. public class CustomersDBManger
  9. {
  10. public static List<Customer> Show() {
  11. return DAL.CustomersDBService.Show();
  12. }
  13. public static bool Add(Customer cu) {
  14. return DAL.CustomersDBService.Add(cu);
  15. }
  16. public static bool shan(int id) {
  17. return DAL.CustomersDBService.shan(id);
  18. }
  19. }
  20. }

WebForm1.aspx

前端部分

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title></title>
  7. <style type="text/css">
  8. .auto-style1 {
  9. height: 25px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div>
  16. <table border="1">
  17. <tr>
  18. <td>编号</td>
  19. <td>客户名</td>
  20. <td>性别</td>
  21. <td>生日</td>
  22. <td>电话</td>
  23. <td>邮件</td>
  24. <td>操作</td>
  25. </tr>
  26. <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
  27. <ItemTemplate>
  28. <tr>
  29. <td><%#Eval("CID") %></td>
  30. <td><%#Eval("CName") %></td>
  31. <td><%#Eval("CGender") %></td>
  32. <td><%#Eval("CBirthday") %></td>
  33. <td><%#Eval("CTel") %></td>
  34. <td><%#Eval("CEmail") %></td>
  35. <td><a href="WebForm2.aspx">新增</a>
  36. <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("CID") %>' CommandName="del" onClientClick="return confirm('确定删除?');" >删除</asp:LinkButton>
  37. </td>
  38. </tr>
  39. </ItemTemplate>
  40. </asp:Repeater>
  41. </table>
  42. </div>
  43. </form>
  44. </body>
  45. </html>

后端部分

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using WebApplication1.Models;
  8. namespace WebApplication1
  9. {
  10. public partial class WebForm1 : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. Repeater1.DataSource= BLL.CustomersDBManger.Show();
  15. Repeater1.DataBind();
  16. }
  17. protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e){
  18. int id = Convert.ToInt32(e.CommandArgument);
  19. if (e.CommandName=="del")
  20. {
  21. bool sta = BLL.CustomersDBManger.shan(id);
  22. string st = sta == true ? "成功" : "失败";
  23. ClientScript.RegisterStartupScript(this.GetType(),"alert","alert('成功')",true);
  24. Repeater1.DataSource = BLL.CustomersDBManger.Show();
  25. Repeater1.DataBind();
  26. }
  27. }
  28. }
  29. }

WebForm2.aspx

前端部分

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title></title>
  7. </head>
  8. <body>
  9. <a href="WebForm1.aspx">返回</a>
  10. <form id="form1" runat="server">
  11. <div>
  12. <asp:Label ID="Label1" runat="server" Text="姓名"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  13. <br /> <asp:Label ID="Label5" runat="server" Text="性别"></asp:Label> <asp:DropDownList ID="DropDownList1" runat="server">
  14. <asp:ListItem></asp:ListItem>
  15. <asp:ListItem></asp:ListItem>
  16. </asp:DropDownList>
  17. <br /> <asp:Label ID="Label2" runat="server" Text="生日"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  18. <br /> <asp:Label ID="Label3" runat="server" Text="电话"></asp:Label><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  19. <br /> <asp:Label ID="Label4" runat="server" Text="邮箱"></asp:Label><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  20. </div>
  21. <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
  22. </form>
  23. </body>
  24. </html>

后端部分

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using WebApplication1.Models;
  8. namespace WebApplication1
  9. {
  10. public partial class WebForm2 : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. try
  18. {
  19. if (TextBox1.Text != "" && DropDownList1.SelectedValue.ToString() != "")
  20. {
  21. Customer model = new Customer();
  22. model.CName = TextBox1.Text;
  23. model.CGender = DropDownList1.SelectedValue.ToString();
  24. model.CBirthday = DateTime.Parse(TextBox2.Text);
  25. model.CTel = TextBox3.Text;
  26. model.CEmail = TextBox4.Text;
  27. if (BLL.CustomersDBManger.Add(model))
  28. {
  29. ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('正确添加'),location.href='WebForm1.aspx'", true);
  30. }
  31. else
  32. {
  33. ClientScript.RegisterStartupScript(this.GetType(), "fail", "alert('错误!')", true);
  34. };
  35. }
  36. else
  37. {
  38. Response.Write("请完善");
  39. }
  40. }
  41. catch (Exception s)
  42. {
  43. Console.WriteLine(s);
  44. }
  45. }
  46. }
  47. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/304811
推荐阅读
相关标签
  

闽ICP备14008679号