回味手写三层-增删改查
先看下全家福吧
切入正题
首先要有一个model,没有对象操作怎么行
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ClassModel { public class ClassModels { private int cId; public int CId { get { return cId; } set { cId = value; } } private string cName; public string CName { get { return cName; } set { cName = value; } } private int cCount; public int CCount { get { return cCount; } set { cCount = value; } } } }
然后要有一个SqlHelper(不然操作数据库要累死了)
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Configuration; 6 using System.Data.SqlClient; 7 using System.Data; 8 9 namespace ClassDal 10 { 11 class SqlHelper 12 { 13 public static string connstr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString; 14 public static SqlDataReader ExcuteReader(string sql, params SqlParameter[] pams) 15 { 16 SqlConnection conn = new SqlConnection(connstr); 17 using (SqlCommand cmd=new SqlCommand(sql,conn)) 18 { 19 if (pams!=null) 20 { 21 cmd.Parameters.AddRange(pams); 22 } 23 if (conn.State==System.Data.ConnectionState.Closed) 24 { 25 conn.Open(); 26 } 27 return cmd.ExecuteReader(); 28 } 29 30 } 31 32 public static DataTable ExcuteDataTable(string sql,params SqlParameter[] pams) 33 { 34 using (SqlDataAdapter adapter=new SqlDataAdapter(sql,connstr)) 35 { 36 if (pams!=null) 37 { 38 adapter.SelectCommand.Parameters.AddRange(pams); 39 } 40 DataTable dt = new DataTable(); 41 adapter.Fill(dt); 42 return dt; 43 } 44 } 45 //增删改 46 public static int ExcuteNonQuery(string sql, params SqlParameter[] pams) 47 { 48 using (SqlConnection conn=new SqlConnection(connstr)) 49 { 50 using (SqlCommand cmd=new SqlCommand(sql,conn)) 51 { 52 if (pams!=null) 53 { 54 cmd.Parameters.AddRange(pams); 55 } 56 if (conn.State==ConnectionState.Closed) 57 { 58 conn.Open(); 59 } 60 return cmd.ExecuteNonQuery(); 61 } 62 } 63 } 64 65 public static object ExcuteScalar(string sql, params SqlParameter[] pams) 66 { 67 using (SqlConnection conn=new SqlConnection(connstr)) 68 { 69 using (SqlCommand cmd=new SqlCommand(sql,conn)) 70 { 71 if (pams!=null) 72 { 73 cmd.Parameters.AddRange(pams); 74 } 75 if (conn.State==ConnectionState.Closed) 76 { 77 conn.Open(); 78 } 79 return cmd.ExecuteScalar(); 80 } 81 } 82 } 83 } 84 85 86 }
然后写 数据访问层(DAL):
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 using System.Data.SqlClient; 7 namespace ClassDal 8 { 9 public class ClassDals 10 { 11 //datetable 获取信息 12 public DataTable GetAll() 13 { 14 string sql = "select *from Classes"; 15 return SqlHelper.ExcuteDataTable(sql); 16 } 17 // datereader 获取信息 18 public SqlDataReader GetAllInfo() 19 { 20 string sql = "select *from Classes"; 21 return SqlHelper.ExcuteReader(sql); 22 } 23 public SqlDataReader GetInfoFromId(ClassModel.ClassModels model) 24 { 25 string sql = "select *from Classes where CID=@id"; 26 return SqlHelper.ExcuteReader(sql, new SqlParameter("@id",model.CId)); 27 } 28 public int DeleteInfo(int id) 29 { 30 string sql = "delete from Classes where CID=@cid"; 31 SqlParameter pam = new SqlParameter("@cid",id); 32 return SqlHelper.ExcuteNonQuery(sql,pam); 33 } 34 //CID, CName, CCount, CImage, CIsDel, CAddTime 35 public int AddInfo(ClassModel.ClassModels model) 36 { 37 38 string sql = "insert into Classes(CName,CCount)values(@CName,@CCount)"; 39 SqlParameter[] pam = new SqlParameter[] { 40 new SqlParameter("@CName",model.CName), 41 new SqlParameter("@CCount",model.CCount) 42 }; 43 return SqlHelper.ExcuteNonQuery(sql,pam); 44 } 45 public int UpdateInfo(ClassModel.ClassModels model) 46 { 47 //update Classes set CName='123',CCount='123' where CID='43' 48 string sql = "update Classes set CName=@name,CCount=@count where CID=@id"; 49 SqlParameter[] pams = new SqlParameter[] { 50 new SqlParameter("@id",model.CId), 51 new SqlParameter("@name",model.CName), 52 new SqlParameter("@count",model.CCount) 53 }; 54 return SqlHelper.ExcuteNonQuery(sql,pams); 55 } 56 57 } 58 }
在写 业务逻辑层(BLL):
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 using ClassDal; 7 using System.Data.SqlClient; 8 9 namespace ClassBll 10 { 11 public class ClassBll 12 { 13 ClassDals dal = new ClassDals(); 14 public List<ClassModel.ClassModels> GetAll() 15 { 16 DataTable da = dal.GetAll(); 17 List<ClassModel.ClassModels> list = new List<ClassModel.ClassModels>(); 18 if (da.Rows.Count > 0) 19 { 20 ClassModel.ClassModels mode = null; 21 foreach (DataRow row in da.Rows) 22 { 23 mode = new ClassModel.ClassModels(); 24 mode.CId = Convert.ToInt32(row["CID"].ToString()); 25 mode.CCount = Convert.ToInt32(row["CCount"].ToString()); 26 mode.CName = row["CName"].ToString(); 27 list.Add(mode); 28 } 29 return list; 30 } 31 else 32 { 33 return null; 34 } 35 } 36 37 public List<ClassModel.ClassModels> GetAllInfo() 38 { 39 List<ClassModel.ClassModels> list = new List<ClassModel.ClassModels>(); 40 SqlDataReader reader = dal.GetAllInfo(); 41 while (reader.Read()) 42 { 43 if (reader.HasRows) 44 { 45 ClassModel.ClassModels model = new ClassModel.ClassModels(); 46 model.CId = reader.GetInt32(0); 47 model.CName = reader.GetString(1); 48 model.CCount = reader.GetInt32(2); 49 list.Add(model); 50 51 } 52 } 53 return list; 54 } 55 public ClassModel.ClassModels GetInfoFromId(ClassModel.ClassModels model) 56 { 57 58 SqlDataReader reader = dal.GetInfoFromId(model); 59 if (reader.Read()) 60 { 61 if (reader.HasRows) 62 { 63 //ClassModel.ClassModels model = new ClassModel.ClassModels(); 64 model.CId = reader.GetInt32(0); 65 model.CName = reader.GetString(1); 66 model.CCount = reader.GetInt32(2); 67 68 69 } 70 } 71 72 return model; 73 } 74 public int DeleteInfo(int id) 75 { 76 return dal.DeleteInfo(id); 77 } 78 public int AddInfo(ClassModel.ClassModels model) 79 { 80 return dal.AddInfo(model); 81 } 82 public int UpdateInfo(ClassModel.ClassModels model) 83 { 84 return dal.UpdateInfo(model); 85 } 86 87 } 88 }
然后要写一个list页面显示数据,不然怎么增删改呢?(好像是废话!)
前台:
View Code
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="List" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title>学生成绩管理系统</title> 8 <style type="text/css"> 9 #tbList 10 { 11 border: 1px solid blue; 12 13 } 14 #tbList th, td 15 { 16 border-bottom: 1px solid blue; 17 border-right: 1px solid blue; 18 19 } 20 </style> 21 </head> 22 <body> 23 <form id="form1" method="post" action="AddInfo.aspx"> 24 <div> 25 <table border="1" cellpadding="1" cellspacing="0"> 26 <tr> 27 <th>编号</th><th>班级名称</th><th>人数</th><th colspan="2">操作</th> 28 </tr> 29 <%=GetAllInfo()%> 30 </table> 31 <input type="submit" name="btnAdd" value="添加" /> 32 </div> 33 </form> 34 </body> 35 </html>
后台:
View Code
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 System.Text; 8 9 public partial class List : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 14 } 15 public string GetAllInfo() 16 { 17 StringBuilder sb = new StringBuilder(); 18 ClassBll.ClassBll bll = new ClassBll.ClassBll(); 19 List<ClassModel.ClassModels> list = bll.GetAllInfo(); 20 foreach (ClassModel.ClassModels model in list) 21 { 22 sb.Append("<tr>"); 23 sb.Append("<td>" + model.CId + "</td>"); 24 sb.Append("<td>" + model.CName + "</td>"); 25 sb.Append("<td>" + model.CCount + "</td>"); 26 sb.Append("<td><a href='Update.aspx?id=" + model.CId + "'>修改</td>"); 27 sb.Append("<td><a href='Delete.ashx?id=" + model.CId + "'>删除</td>"); 28 sb.Append("</tr>"); 29 } 30 return sb.ToString(); 31 } 32 }
然后先写增加 吧!要不然一会辛辛苦苦写的数据 都删完了!
这里完全可以把 增加 写到 list页面的,太懒了,回头改下
前台:
View Code
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddInfo.aspx.cs" Inherits="AddInfo" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 </head> 9 <body> 10 <form id="form1" method="post" action="AddInfo.aspx"> 11 <div> 12 班级名称: <input type="text" name="txtCName" value="" /><br /> 13 班级人数: <input type="text" name="txtCCount" value="" /><br /> 14 <input type="hidden" name="isPostBack" value="2" /> 15 <input type="submit" name="btnAdd" value="保存" /> 16 </div> 17 </form> 18 </body> 19 </html>
后台:
View Code
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 8 public partial class AddInfo : System.Web.UI.Page 9 { 10 protected void Page_Load(object sender, EventArgs e) 11 { 12 string name = Request.Form["txtCName"];//txtCCount isPostBack 13 string countstr = Request.Form["txtCCount"]; 14 string ispostBack=Request.Form["isPostBack"]; 15 if (!string.IsNullOrEmpty(ispostBack)) 16 { 17 int count = 0; 18 if (int.TryParse(countstr,out count)) 19 { 20 ClassBll.ClassBll bll = new ClassBll.ClassBll(); 21 ClassModel.ClassModels model = new ClassModel.ClassModels(); 22 model.CName = name; 23 model.CCount = count; 24 if (bll.AddInfo(model)>0) 25 { 26 Response.Write("<script type='text/javascript'>alert('添加成功!');window.location='List.aspx'</script>"); 27 } 28 else 29 { 30 Response.Write("添加失败!"); 31 } 32 33 } 34 } 35 } 36 }
该写删除了 一个一般处理程序搞定!
View Code
1 <%@ WebHandler Language="C#" Class="Delete" %> 2 3 using System; 4 using System.Web; 5 6 public class Delete : IHttpHandler { 7 8 public void ProcessRequest (HttpContext context) { 9 context.Response.ContentType = "text/html"; 10 string id=context.Request.QueryString["id"]; 11 if (!string.IsNullOrEmpty(id)) 12 { 13 int sid = 0; 14 if (int.TryParse(id,out sid)) 15 { 16 ClassBll.ClassBll bll = new ClassBll.ClassBll(); 17 18 if (bll.DeleteInfo(sid)>0) 19 { 20 context.Response.Write("<script type='text/javascript'>alert('删除成功!');window.location='List.aspx'</script>"); 21 } 22 else 23 { 24 context.Response.Write("删除失败!"); 25 } 26 } 27 } 28 } 29 30 public bool IsReusable { 31 get { 32 return false; 33 } 34 } 35 36 }
最后就是修改了,这个才是有点意思的
前台:
View Code
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update.aspx.cs" Inherits="Update" %> 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" method="post" action="Update.aspx"> 10 <div> 11 班级名称: 12 <%--用方法返回一个字符串--%> 13 <%-- <input type="text" name="txtCName" value="<%=CCNameInfo() %>" /><br />--%> 14 <input type="text" name="txtCName" value="<%=model.CName %>" /><br /> 15 班级人数: 16 <input type="text" name="txtCCount" value="<%=model.CCount %>" /><br /> 17 <input type="hidden" name="isPostBack" value="<%=model.CId %>" /> 18 <input type="submit" name="btnUpdate" value="保存" /> 19 </div> 20 </form> 21 </body> 22 </html>
后台:
View Code
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 8 public partial class Update : System.Web.UI.Page 9 { 10 ClassBll.ClassBll bll = new ClassBll.ClassBll(); 11 protected ClassModel.ClassModels model = new ClassModel.ClassModels(); 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 string isPostback = Request.Form["isPostBack"]; 15 //如果是准备提交保存 16 if (!string.IsNullOrEmpty(isPostback)) 17 { 18 int idCount = 0; 19 if (int.TryParse(isPostback,out idCount)) 20 { 21 string cName = Request.Form["txtCName"];// txtCName txtCCount 22 string cCount = Request.Form["txtCCount"]; 23 model.CId = idCount; 24 model.CName = cName; 25 model.CCount =Convert.ToInt32(cCount); 26 if (bll.UpdateInfo(model)>0) 27 { 28 Response.Write("<script type='text/javascript'>alert('保存成功!');window.location='List.aspx'</script>"); 29 } 30 else 31 { 32 Response.Write("修改失败!"); 33 } 34 } 35 } 36 else//如果是第一次访问 37 { 38 string id = Request.QueryString["id"]; 39 40 int idcount = 0; 41 if (int.TryParse(id, out idcount)) 42 { 43 model.CId = idcount; 44 //查寻数据并存到model中 45 model = bll.GetInfoFromId(model); 46 } 47 } 48 49 50 51 } 52 //protected string CId() 53 //{ 54 // return model.CId.ToString(); 55 //} 56 //protected string CCountInfo() 57 //{ 58 // return model.CCount.ToString(); 59 //} 60 //protected string CCNameInfo() 61 //{ 62 // return model.CName; 63 //} 64 }
其实 修改 和 添加完全可以共用一个页面,或者一个div就行了,这里我就分开写了,看着简单点!
对了别忘了配置webconfig 哦!
View Code
1 <?xml version="1.0"?> 2 <!-- 3 有关如何配置 ASP.NET 应用程序的详细信息,请访问 4 http://go.microsoft.com/fwlink/?LinkId=169433 5 --> 6 <configuration> 7 <connectionStrings> 8 <add name="sql" connectionString="Data Source=.;Initial Catalog=ProManager;Integrated Security=true;"/> 9 </connectionStrings> 10 <system.web> 11 <compilation debug="true" targetFramework="4.0"/> 12 </system.web> 13 </configuration>
这样就大功搞成了,挺简单的吧!