C#教程之自己动手写映射第二节[实例]
在我们平时做项目的时候与数据交互时,涉及的操作大部分为 “增、删、改、查[又分为分页与列表]”下面就以示例来显示我们封装底层后的使用实例。
一、新增与修改
新增与修改在表现上几乎是一样的,我们在这个示例中把新增和修改放到了一个页面进行编辑,根据是否传入参数来判断是“新增”还是“修改”,那么这个页面就要和列表页有一个传入值的约定,如:“添加”链接时我们<a href="Save.aspx">添加</a>,修改时<a href="Save.aspx?ID=<%#Eval("ID") %>">修改</a>。
图示如下:
代码如下:
UI层
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Save.aspx.cs" Inherits="CSharp.WebSite.Save" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html xmlns="http://www.w3.org/1999/xhtml"> 5 <head runat="server"> 6 <title></title> 7 </head> 8 <body> 9 <form id="form1" runat="server"> 10 员工姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> 11 登陆密码:<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br /> 12 部门:<asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox><br /> 13 职位:<asp:TextBox ID="txtPosition" runat="server"></asp:TextBox><br /> 14 <asp:Button ID="btnSubmit" runat="server" Text="确定" OnClick="btnSubmit_Click" /> 15 <asp:Button ID="btnReset" runat="server" Text="取消" OnClick="btnReset_Click" /><br /> 16 </form> 17 </body> 18 </html> 19 20 //CodeBehind 21 22 /* 23 * 24 * 创建人:李林峰 25 * 26 * 时 间:2012-07-22 27 * 28 * 描 述:员工添加与修改 29 * 30 */ 31 32 using System; 33 using System.Collections.Generic; 34 using System.Web; 35 using System.Web.UI; 36 using System.Web.UI.WebControls; 37 38 namespace CSharp.WebSite 39 { 40 /// <summary> 41 /// 员工添加与修改 42 /// </summary> 43 public partial class Save : System.Web.UI.Page 44 { 45 #region 页面加载 46 protected void Page_Load(object sender, EventArgs e) 47 { 48 if (!Page.IsPostBack) 49 { 50 //修改时根据编号取出实体信息 51 if (!string.IsNullOrEmpty(Request.QueryString["ID"])) 52 { 53 //获取用户 54 GetEmployee(); 55 } 56 } 57 } 58 #endregion 59 60 #region 方法 61 /// <summary> 62 /// 获取用户 63 /// </summary> 64 protected void GetEmployee() 65 { 66 Model.Employee employee = new Model.Employee(); 67 employee.ID = Convert.ToInt32(Request.QueryString["ID"]); 68 employee = BLL.Employee.Get(employee); 69 this.txtName.Text = employee.Name; 70 this.txtPassword.Text = employee.Password; 71 this.txtDepartment.Text = employee.Department; 72 this.txtPosition.Text = employee.Position; 73 } 74 #endregion 75 76 #region 事件 77 /// <summary> 78 /// 提交 79 /// </summary> 80 /// <param name="sender"></param> 81 /// <param name="e"></param> 82 protected void btnSubmit_Click(object sender, EventArgs e) 83 { 84 Model.Employee employee = new Model.Employee(); 85 //如果传入的编号不为空,说明是修改对象,则先根据编号取出对象 86 if (!string.IsNullOrEmpty(Request.QueryString["ID"])) 87 { 88 employee.ID = Convert.ToInt32(Request.QueryString["ID"]); 89 employee = BLL.Employee.Get(employee); 90 } 91 employee.Name = this.txtName.Text; 92 employee.Password = this.txtPassword.Text; 93 employee.Department = this.txtDepartment.Text; 94 employee.Position = this.txtPosition.Text; 95 //修改对象 96 if (!string.IsNullOrEmpty(Request.QueryString["ID"])) 97 { 98 if (BLL.Employee.Save(employee)) 99 MessageBox.ShowAndRedirect(this, "修改成功!", "List.aspx"); 100 else 101 MessageBox.Show(this, "修改失败,请与管理员联系!"); 102 } 103 else//新增 104 { 105 if (BLL.Employee.Add(employee)) 106 MessageBox.ShowAndRedirect(this, "添加成功!", "List.aspx"); 107 else 108 MessageBox.Show(this, "添加失败,请与管理员联系!"); 109 } 110 } 111 112 /// <summary> 113 /// 重置 114 /// </summary> 115 /// <param name="sender"></param> 116 /// <param name="e"></param> 117 protected void btnReset_Click(object sender, EventArgs e) 118 { 119 //如果控件多,可以写个帮助类,遍历清空 120 this.txtName.Text = ""; 121 this.txtPassword.Text = ""; 122 this.txtDepartment.Text = ""; 123 this.txtPosition.Text = ""; 124 } 125 #endregion 126 } 127 }
二、列表
列表展现用到Aspnetpager控件,在修改时向Save.aspx传入将要修改的编号,删除时用OnCommand事件,我们可以通过CommandName把要删除的编号传到服务端进行处理。
图示如下:
代码如下:
UI层
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="CSharp.WebSite.List" %> 2 3 <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <table> 12 <tr> 13 <td colspan="5"> 14 <a href="Save.aspx">添加</a> 15 </td> 16 </tr> 17 <tr> 18 <td colspan="5"> 19 姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><asp:Button ID="btnSearch" 20 runat="server" Text="查询" OnClick="btnSearch_OnClick" /> 21 </td> 22 </tr> 23 <tr> 24 <td> 25 编号 26 </td> 27 <td> 28 姓名 29 </td> 30 <td> 31 部门 32 </td> 33 <td> 34 职位 35 </td> 36 <td> 37 操作 38 </td> 39 </tr> 40 <asp:Repeater ID="repList" runat="server"> 41 <ItemTemplate> 42 <tr> 43 <td> 44 <%#(Container.ItemIndex+1)+(Pager.PageSize * (Pager.CurrentPageIndex - 1)) %> 45 </td> 46 <td> 47 <%#Eval("Name") %> 48 </td> 49 <td> 50 <%#Eval("Department")%> 51 </td> 52 <td> 53 <%#Eval("Position")%> 54 </td> 55 <td> 56 <a href="Save.aspx?ID=<%#Eval("ID") %>">修改</a> 57 <asp:LinkButton ID="lbtRemove" runat="server" CommandName='<%#Eval("ID") %>' OnCommand="lbtnRemoveCommand" 58 OnClientClick="return confirm('确定删除吗?');" CausesValidation="False">删除 59 </asp:LinkButton> 60 </td> 61 </tr> 62 </ItemTemplate> 63 </asp:Repeater> 64 </table> 65 <webdiyer:AspNetPager ID="Pager" CssClass="Pager" runat="server" AlwaysShow="True" 66 FirstPageText="首页" InvalidPageIndexErrorMessage="请输入数字页码!" LastPageText="末页" 67 NextPageText="下一页" PageIndexOutOfRangeErrorMessage="页码超出范围!" PrevPageText="上一页" 68 ShowNavigationToolTip="True" SubmitButtonText="确定" CenterCurrentPageButton="True" 69 PageIndexBoxType="TextBox" PageSize="2" ShowPageIndexBox="never" TextAfterPageIndexBox=" 页 " 70 TextBeforePageIndexBox="转到 " OnPageChanged="Pager_PageChanged" HorizontalAlign="NotSet" 71 CurrentPageButtonClass=""> 72 </webdiyer:AspNetPager> 73 </form> 74 </body> 75 </html> 76 77 //CodeBehind 78 79 /* 80 * 81 * 创建人:李林峰 82 * 83 * 时 间:2012-07-22 84 * 85 * 描 述:员工列表 86 * 87 */ 88 89 using System; 90 using System.Collections.Generic; 91 using System.Web; 92 using System.Web.UI; 93 using System.Web.UI.WebControls; 94 95 namespace CSharp.WebSite 96 { 97 /// <summary> 98 /// 员工列表 99 /// </summary> 100 public partial class List : System.Web.UI.Page 101 { 102 #region 页面加载 103 /// <summary> 104 /// 面页加载 105 /// </summary> 106 /// <param name="sender"></param> 107 /// <param name="e"></param> 108 protected void Page_Load(object sender, EventArgs e) 109 { 110 if (!Page.IsPostBack) 111 { 112 //获取列表 113 GetList(); 114 } 115 } 116 #endregion 117 118 #region 方法 119 /// <summary> 120 /// 根据查询条件获取用户列表 121 /// </summary> 122 protected void GetList() 123 { 124 int TotalCount; 125 this.repList.DataSource = BLL.Employee.GetList(this.txtName.Text, this.Pager.PageSize, Pager.PageSize * (Pager.CurrentPageIndex - 1), out TotalCount); 126 this.repList.DataBind(); 127 Pager.RecordCount = TotalCount; 128 } 129 #endregion 130 131 #region 事件 132 /// <summary> 133 /// 分页 134 /// </summary> 135 /// <param name="sender"></param> 136 /// <param name="e"></param> 137 protected void Pager_PageChanged(object sender, EventArgs e) 138 { 139 GetList(); 140 } 141 142 /// <summary> 143 /// 查询 144 /// </summary> 145 /// <param name="sender"></param> 146 /// <param name="e"></param> 147 protected void btnSearch_OnClick(object sender, EventArgs e) 148 { 149 GetList(); 150 } 151 152 /// <summary> 153 /// 移除 154 /// </summary> 155 /// <param name="sender"></param> 156 /// <param name="e"></param> 157 protected void lbtnRemoveCommand(object sender, CommandEventArgs e) 158 { 159 Model.Employee employee = new Model.Employee(); 160 employee.ID = Convert.ToInt32(e.CommandName); 161 if (BLL.Employee.Remove(employee)) 162 MessageBox.Show(this, "删除成功!"); 163 else 164 MessageBox.Show(this, "删除失败!"); 165 //重新加载列表 166 GetList(); 167 } 168 #endregion 169 } 170 }
三、内容
根据编号取出内容
图示如下:
代码如下:
UI层
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Detail.aspx.cs" Inherits="CSharp.WebSite.Detail" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html xmlns="http://www.w3.org/1999/xhtml"> 5 <head id="Head1" runat="server"> 6 <title></title> 7 </head> 8 <body> 9 <form id="form1" runat="server"> 10 员工姓名:<asp:Literal ID="litName" runat="server"></asp:Literal><br /> 11 登陆密码:<asp:Literal ID="litPassword" runat="server"></asp:Literal><br /> 12 部门:<asp:Literal ID="litDepartment" runat="server"></asp:Literal><br /> 13 职位:<asp:Literal ID="litPosition" runat="server"></asp:Literal> 14 </form> 15 </body> 16 </html> 17 18 //CodeBehind 19 20 /* 21 * 22 * 创建人:李林峰 23 * 24 * 时 间:2012-07-22 25 * 26 * 描 述:员工详细 27 * 28 */ 29 30 using System; 31 using System.Collections.Generic; 32 using System.Web; 33 using System.Web.UI; 34 using System.Web.UI.WebControls; 35 36 namespace CSharp.WebSite 37 { 38 /// <summary> 39 /// 员工详细 40 /// </summary> 41 public partial class Detail : System.Web.UI.Page 42 { 43 #region 页面加载 44 protected void Page_Load(object sender, EventArgs e) 45 { 46 if (!Page.IsPostBack) 47 { 48 //修改时根据编号取出实体信息 49 if (!string.IsNullOrEmpty(Request.QueryString["ID"])) 50 { 51 //获取用户 52 GetEmployee(); 53 } 54 } 55 } 56 #endregion 57 58 #region 方法 59 /// <summary> 60 /// 获取用户 61 /// </summary> 62 protected void GetEmployee() 63 { 64 Model.Employee employee = new Model.Employee(); 65 employee.ID = Convert.ToInt32(Request.QueryString["ID"]); 66 employee = BLL.Employee.Get(employee); 67 this.litName.Text = employee.Name; 68 this.litPassword.Text = employee.Password; 69 this.litDepartment.Text = employee.Department; 70 this.litPosition.Text = employee.Position; 71 } 72 #endregion 73 } 74 }
四、业务逻辑层
代码如下:
1 /* 2 * 3 * 创建人:李林峰 4 * 5 * 时 间:2012-7-22 6 * 7 * 描 述:员工 8 * 9 */ 10 11 using System.Collections.Generic; 12 13 namespace CSharp.BLL 14 { 15 /// <summary> 16 /// 员工 17 /// </summary> 18 public class Employee 19 { 20 /// <summary> 21 /// 添加实体 22 /// </summary> 23 /// <param name="employee">传入的实体</param> 24 public static bool Add(Model.Employee employee) 25 { 26 return DAL.Employee.Add(employee); 27 } 28 29 /// <summary> 30 /// 修改实体 31 /// </summary> 32 /// <param name="employee">传入的实体</param> 33 public static bool Save(Model.Employee employee) 34 { 35 return DAL.Employee.Save(employee); 36 } 37 38 /// <summary> 39 /// 移除实体(根据实体编号移除一条记录) 40 /// </summary> 41 /// <param name="employee">传入的实体,必须含有编号</param> 42 public static bool Remove(Model.Employee employee) 43 { 44 return DAL.Employee.Remove(employee); 45 } 46 47 /// <summary> 48 /// 根据编号获取实体 49 /// </summary> 50 /// <param name="employee">传入的实体,必须含有编号,根据编号获取</param> 51 /// <returns>返回得到的实体</returns> 52 public static Model.Employee Get(Model.Employee employee) 53 { 54 return DAL.Employee.Get(employee); 55 } 56 57 /// <summary> 58 /// 获取列表(无条件分页获取全部列表) 59 /// </summary> 60 /// <param name="PageSize">每页的记录数,例如:每页显示10条,则传入 10 </param> 61 /// <param name="CurrentCount">当前是第几页</param> 62 /// <param name="TotalCount">输出参数,返回总记录数</param> 63 /// <returns>返回实体列表</returns> 64 public static List<Model.Employee> GetList(string Name, int PageSize, int CurrentCount, out int TotalCount) 65 { 66 return DAL.Employee.GetList(Name, PageSize, CurrentCount, out TotalCount); 67 } 68 } 69 }
五、数据操作层
代码如下:
1 /* 2 * 3 * 创建人:李林峰 4 * 5 * 时 间:2012-7-22 6 * 7 * 描 述:员工 8 * 9 */ 10 11 using System.Data; 12 using System.Data.SqlClient; 13 using System.Collections.Generic; 14 using HZYT.DBUtility; 15 16 namespace CSharp.DAL 17 { 18 /// <summary> 19 /// 员工 20 /// </summary> 21 public class Employee 22 { 23 /// <summary> 24 /// 添加实体 25 /// </summary> 26 /// <param name="employee">传入的实体</param> 27 /// <returns>操作数据库时返回的操作行数,如果大于 "0" 则返回 true 否则返回 false</returns> 28 public static bool Add(CSharp.Model.Employee employee) 29 { 30 return ORM.Add(employee, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 31 } 32 33 /// <summary> 34 /// 修改实体 35 /// </summary> 36 /// <param name="employee">传入的实体</param> 37 /// <returns>操作数据库时返回的操作行数,如果大于 "0" 则返回 true 否则返回 false</returns> 38 public static bool Save(Model.Employee employee) 39 { 40 return ORM.Save(employee, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 41 } 42 43 /// <summary> 44 /// 移除实体(根据实体编号移除一条记录) 45 /// </summary> 46 /// <param name="employee">传入的实体,必须含有编号</param> 47 /// <returns>操作数据库时返回的操作行数,如果大于 "0" 则返回 true 否则返回 false</returns> 48 public static bool Remove(Model.Employee employee) 49 { 50 return ORM.Remove(employee, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 51 } 52 53 /// <summary> 54 /// 移除实体(根据实体列表移除多条记录) 55 /// </summary> 56 /// <param name="listEmployee">传入的实体列表,必须都含有编号</param> 57 /// <returns>操作数据库时返回的操作行数,如果大于 "0" 则返回 true 否则返回 false</returns> 58 public static bool Remove(List<Model.Employee> listEmployee) 59 { 60 List<object> listObject = new List<object>(); 61 foreach (Model.Employee model in listEmployee) 62 { 63 listObject.Add(model); 64 } 65 return ORM.Remove(listObject, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 66 } 67 68 /// <summary> 69 /// 根据编号获取实体 70 /// </summary> 71 /// <param name="employee">传入的实体,必须含有编号,根据编号获取</param> 72 /// <returns>返回得到的实体</returns> 73 public static Model.Employee Get(Model.Employee employee) 74 { 75 return (Model.Employee)ORM.Get(employee, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 76 } 77 78 /// <summary> 79 /// 获取列表(无条件获取全部列表) 80 /// </summary> 81 /// <returns>返回实体列表</returns> 82 public static List<Model.Employee> GetList() 83 { 84 List<Model.Employee> returnList = new List<Model.Employee>(); 85 List<object> tempList = ORM.GetList(new Model.Employee(), Constant.ASSEMBLYPATH, Constant.CONNSTRING); 86 return GetList(tempList); 87 } 88 89 /// <summary> 90 /// 获取列表(无条件分页获取全部列表) 91 /// </summary> 92 /// <param name="PageSize">每页的记录数,例如:每页显示10条,则传入 10 </param> 93 /// <param name="CurrentCount">当前是第几页</param> 94 /// <param name="TotalCount">输出参数,返回总记录数</param> 95 /// <returns>返回实体列表</returns> 96 public static List<Model.Employee> GetList(int PageSize, int CurrentCount, out int TotalCount) 97 { 98 List<Model.Employee> returnList = new List<Model.Employee>(); 99 List<object> tempList = ORM.GetList(new Model.Employee(), PageSize, CurrentCount, out TotalCount, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 100 return GetList(tempList); 101 } 102 103 /// <summary> 104 /// 获取列表(无条件分页获取全部列表) 105 /// </summary> 106 /// <param name="PageSize">每页的记录数,例如:每页显示10条,则传入 10 </param> 107 /// <param name="CurrentCount">当前是第几页</param> 108 /// <param name="TotalCount">输出参数,返回总记录数</param> 109 /// <returns>返回实体列表</returns> 110 public static List<Model.Employee> GetList(string Name, int PageSize, int CurrentCount, out int TotalCount) 111 { 112 string WHERE = " 1=1 "; 113 if (Name.Trim() != string.Empty) 114 { 115 WHERE += " AND Name LIKE '%" + Name + "%'"; 116 } 117 return GetList(PageSize, CurrentCount, WHERE, out TotalCount); 118 } 119 120 #region 获取列表[私有方法] 121 /// <summary> 122 /// 获取列表(根据查询条件获取全部列表) 123 /// </summary> 124 /// <param name="Where">SQL拼接的查询条件字条串,例如: " ID < 100 " </param> 125 /// <returns>返回实体列表</returns> 126 private static List<Model.Employee> GetList(string Where) 127 { 128 List<Model.Employee> returnList = new List<Model.Employee>(); 129 List<object> tempList = ORM.GetList(new Model.Employee(), Where, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 130 return GetList(tempList); 131 } 132 133 /// <summary> 134 /// 获取列表(无条件分页获取全部列表) 135 /// </summary> 136 /// <param name="PageSize">每页的记录数,例如:每页显示10条,则传入 10 </param> 137 /// <param name="CurrentCount">当前是第几页</param> 138 /// <param name="Where">SQL拼接的查询条件字条串,例如: " ID < 100 " </param> 139 /// <param name="TotalCount">输出参数,返回总记录数</param> 140 /// <returns>返回实体列表</returns> 141 private static List<Model.Employee> GetList(int PageSize, int CurrentCount, string Where, out int TotalCount) 142 { 143 List<Model.Employee> returnList = new List<Model.Employee>(); 144 List<object> tempList = ORM.GetList(new Model.Employee(), PageSize, CurrentCount, Where, out TotalCount, Constant.ASSEMBLYPATH, Constant.CONNSTRING); 145 return GetList(tempList); 146 } 147 #endregion 148 149 #region 类型转换[私有方法] 150 /// <summary> 151 /// 将对象类型转换成为Employee类型 152 /// </summary> 153 /// <param name="objList"></param> 154 /// <returns>返回实体列表</returns> 155 private static List<Model.Employee> GetList(List<object> objList) 156 { 157 List<Model.Employee> returnList = new List<Model.Employee>(); 158 foreach (object tempobject in objList) 159 { 160 returnList.Add((Model.Employee)tempobject); 161 } 162 return returnList; 163 } 164 #endregion 165 } 166 }
六、实体层
实体层有两个类Employee.cs与EmployeeMapping.cs,一个用来存储在各个层之间传输的数据信息,一个来描述实体与数据库的对应关系。
代码如下:
1 /* 2 * 3 * 创建人:李林峰 4 * 5 * 时 间:2012-7-122 6 * 7 * 描 述:员工 8 * 9 */ 10 11 using System; 12 13 namespace CSharp.Model 14 { 15 /// <summary> 16 /// 员工 17 /// </summary> 18 public class Employee 19 { 20 public int ID { get; set; } //编号 21 public string Name { get; set; } //姓名 22 public string Password { get; set; } //密码 23 public string Department { get; set; } //部门 24 public string Position { get; set; } //职位 25 } 26 } 27 28 29 /* 30 * 31 * 创建人:李林峰 32 * 33 * 时 间:2012-7-22 34 * 35 * 描 述:员工映射 36 * 37 */ 38 39 using System.Collections.Generic; 40 41 namespace CSharp.Model 42 { 43 /// <summary> 44 /// 员工映射 45 /// </summary> 46 public class EmployeeMapping 47 { 48 /// <summary> 49 /// 表名称 50 /// </summary> 51 public static string GetTableName() 52 { 53 return "Employee"; 54 } 55 56 /// <summary> 57 /// 主键名称 58 /// </summary> 59 public static Dictionary<string, string> GetIdentityMapping() 60 { 61 Dictionary<string, string> Identity = new Dictionary<string, string>(); 62 Identity.Add("ID", "ID"); 63 return Identity; 64 } 65 66 /// <summary> 67 /// 基础字段 68 /// </summary> 69 public static Dictionary<string, string> GetBaseFieldMapping() 70 { 71 Dictionary<string, string> BaseField = new Dictionary<string, string>(); 72 BaseField.Add("Name", "Name"); 73 BaseField.Add("Password", "Password"); 74 BaseField.Add("Department", "Department"); 75 BaseField.Add("Position", "Position"); 76 return BaseField; 77 } 78 } 79 }
七、总结
通过上面的实例我们可以看出惟一不同之处在于我们的数据操作层只用了很少的SQL就完成了“增、删、改、查”的四项操作功能,而在实体层里我们多了一个实例与数据库的映射关系类。这样我们在开发中配上代码生成工具就很“快速、简洁”的完成了工作。
八、代码下载