一、常用技术概括及介绍
1. SQL server:处理数据库的设计
2. asp.net
3. html :前端网页
4. css :网页的布局设计
5. JavaScript :能够更好的操作页面
6. jQuery :
7. ajax :处理局部刷新请求
二、分层介绍 (类库)
1.bll:业务层、
2.dal:数据层、
3.model:对应数据库的表、
4.common:公共方法
5.webapp:新建的app
6.webapp自带的配置文件
创建好之后如下:
三、实现步骤
1. 数据层中两个类,对应封装增删改查语句
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace CZBK.ItcastProject.DAL { public class SqlHelper { //getdatetable方法获取整个表 //参数1.sql语句 2. 判断是sql语句还是存储过程 3. 传递的参数 private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //读取配置文件中的字符串 public static DataTable GetDataTable(string sql,CommandType type,params SqlParameter[]pars) { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlDataAdapter apter = new SqlDataAdapter(sql, conn)) { if (pars != null) { apter.SelectCommand.Parameters.AddRange(pars); } apter.SelectCommand.CommandType = type; DataTable da = new DataTable(); apter.Fill(da); return da; } } } //获取受影响行数,ExecuteNonQuery 主要用在插入,更新,删除 一般情况用在查询的时候返回的是-1 public static int ExecuteNonquery(string sql, CommandType type, params SqlParameter[] pars) {
using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { if (pars != null) { cmd.Parameters.AddRange(pars); } cmd.CommandType = type; conn.Open(); return cmd.ExecuteNonQuery(); } } } } }
1 using CZBK.ItcastProject.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Data; 8 using System.Data.SqlClient; 9 namespace CZBK.ItcastProject.DAL 10 { 11 public class UserInfoDal 12 { 13 /// <summary> 14 /// 获取用户列表 15 /// </summary> 16 /// <returns></returns> 17 public List<UserInfo> GetList() 18 { 19 string sql = "select * from UserInfo"; 20 DataTable da = SqlHelper.GetDataTable(sql, CommandType.Text); 21 List<UserInfo> list = null; 22 if (da.Rows.Count > 0) 23 { 24 list = new List<UserInfo>(); 25 UserInfo userInfo = null; 26 foreach (DataRow row in da.Rows) 27 { 28 userInfo = new UserInfo(); 29 LoadEntity(userInfo, row); 30 list.Add(userInfo); 31 } 32 } 33 return list; 34 } 35 /// <summary> 36 /// 添加用户信息 37 /// </summary> 38 /// <param name="userInfo"></param> 39 /// <returns></returns> 40 public int AddUserInfo(UserInfo userInfo) 41 { 42 string sql = "insert into UserInfo(UserName,UserPass,RegTime,Email) values(@UserName,@UserPass,@RegTime,@Email)"; 43 SqlParameter[] pars = { 44 new SqlParameter("@UserName",SqlDbType.NVarChar,32), 45 new SqlParameter("@UserPass",SqlDbType.NVarChar,32), 46 new SqlParameter("@RegTime",SqlDbType.DateTime), 47 new SqlParameter("@Email",SqlDbType.NVarChar,32) 48 }; 49 pars[0].Value = userInfo.UserName; 50 pars[1].Value = userInfo.UserPass; 51 pars[2].Value = userInfo.RegTime; 52 pars[3].Value = userInfo.Email; 53 return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars); 54 } 55 56 /// <summary> 57 /// 根据ID删除用户的信息 58 /// </summary> 59 /// <param name="id"></param> 60 /// <returns></returns> 61 public int DeleteUserInfo(int id) 62 { 63 string sql = "delete from UserInfo where ID=@ID"; 64 SqlParameter[] pars = { 65 new SqlParameter("@ID",SqlDbType.Int) 66 }; 67 pars[0].Value = id; 68 return SqlHelper.ExecuteNonquery(sql,CommandType.Text,pars); 69 } 70 71 /// <summary> 72 /// 修改用户信息 73 /// </summary> 74 /// <param name="userInfo"></param> 75 /// <returns></returns> 76 public int EditUserInfo(UserInfo userInfo) 77 { 78 string sql = "update UserInfo set UserName=@UserName,UserPass=@UserPass,RegTime=@RegTime,Email=@Email where ID=@ID"; 79 SqlParameter[] pars = { 80 new SqlParameter("@UserName",SqlDbType.NVarChar,32), 81 new SqlParameter("@UserPass",SqlDbType.NVarChar,32), 82 new SqlParameter("@RegTime",SqlDbType.DateTime), 83 new SqlParameter("@Email",SqlDbType.NVarChar,32), 84 new SqlParameter("@ID",SqlDbType.Int) 85 }; 86 pars[0].Value = userInfo.UserName; 87 pars[1].Value = userInfo.UserPass; 88 pars[2].Value = userInfo.RegTime; 89 pars[3].Value = userInfo.Email; 90 pars[4].Value = userInfo.Id; 91 return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars); 92 } 93 94 /// <summary> 95 /// 根据用户的编号,获取用户的信息 96 /// </summary> 97 /// <param name="id"></param> 98 /// <returns></returns> 99 public UserInfo GetUserInfo(int id) 100 { 101 string sql = "select * from UserInfo where ID=@ID"; 102 SqlParameter[] pars = { 103 new SqlParameter("@ID",SqlDbType.Int) 104 }; 105 pars[0].Value = id; 106 DataTable da=SqlHelper.GetDataTable(sql, CommandType.Text, pars); 107 UserInfo userInfo = null; 108 if (da.Rows.Count > 0) 109 { 110 userInfo = new UserInfo(); 111 LoadEntity(userInfo, da.Rows[0]); 112 } 113 return userInfo; 114 } 115 116 117 private void LoadEntity(UserInfo userInfo, DataRow row) 118 { 119 userInfo.UserName = row["UserName"] != DBNull.Value ? row["UserName"].ToString() : string.Empty; 120 userInfo.UserPass = row["UserPass"] != DBNull.Value ? row["UserPass"].ToString() : string.Empty; 121 userInfo.Email = row["Email"] != DBNull.Value ? row["Email"].ToString() : string.Empty; 122 userInfo.Id = Convert.ToInt32(row["ID"]); 123 userInfo.RegTime = Convert.ToDateTime(row["RegTime"]); 124 } 125 } 126 }
model层中添加与列相同的表字段。示例代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.ItcastProject.Model { public class UserInfo { public int Id { get; set; } public string UserName { get; set; } public string UserPass { get; set; } public DateTime RegTime { get; set; } public string Email { get; set; } } }
2. 业务层中写一个调用对象方法的方法
1 using CZBK.ItcastProject.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using CZBK.ItcastProject.DAL; 8 namespace CZBK.ItcastProject.BLL 9 { 10 public class UserInfoService 11 { 12 UserInfoDal UserInfoDal = new UserInfoDal(); 13 /// <summary> 14 /// 返回数据列表 15 /// </summary> 16 /// <returns></returns> 17 public List<UserInfo> GetList() 18 { 19 return UserInfoDal.GetList(); 20 } 21 /// <summary> 22 /// 添加数据 23 /// </summary> 24 /// <param name="userInfo"></param> 25 /// <returns></returns> 26 public bool AddUserInfo(UserInfo userInfo) 27 { 28 return UserInfoDal.AddUserInfo(userInfo)>0; 29 } 30 /// <summary> 31 /// 根据ID删除用户的信息 32 /// </summary> 33 /// <param name="id"></param> 34 /// <returns></returns> 35 public bool DeleteUserInfo(int id) 36 { 37 return UserInfoDal.DeleteUserInfo(id) > 0; 38 } 39 /// <summary> 40 /// 修改用户信息 41 /// </summary> 42 /// <param name="userInfo"></param> 43 /// <returns></returns> 44 public bool EditUserInfo(UserInfo userInfo) 45 { 46 return UserInfoDal.EditUserInfo(userInfo) > 0; 47 } 48 /// <summary> 49 /// 根据用户的编号,获取用户的信息 50 /// </summary> 51 /// <param name="id"></param> 52 /// <returns></returns> 53 public UserInfo GetUserInfo(int id) 54 { 55 return UserInfoDal.GetUserInfo(id); 56 } 57 58 } 59 }
3. 新建一个html页面,
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <link href="Css/tableStyle.css" rel="stylesheet" /> <script src="Js/jquery-1.7.1.js"></script> <script type="text/javascript"> $(function () { $(".deletes").click(function () { if (!confirm("确定要删除吗?")) { return false; } }); }); </script> </head> <body> <a href="AddUserInfo.html">添加</a> <table> <tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>时间</th><th>删除</th><th>详细</th><th>编辑</th></tr> @tbody </table> </body> </html>
4. 新建一个一般处理程序
using CZBK.ItcastProject.Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace CZBK.ItcastProject.WebApp { /// <summary> /// UserInfoList 的摘要说明 /// </summary> public class UserInfoList : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //new一个业务层的类库对象 BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); //调用获取全部列表的方法 List<UserInfo> list= UserInfoService.GetList(); //快捷操作字符串的方法 StringBuilder sb = new StringBuilder(); //将遍历到的数据拼接成一个新的字符串 foreach (UserInfo userInfo in list) { sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><a href='DeleteUser.ashx?id={0}' class='deletes'>删除</a></td><td><a href='ShowDetail.ashx?uid={0}'>详细</a></td><td><a href='ShowEdit.ashx?id={0}'>编辑</a></td></tr>",userInfo.Id,userInfo.UserName,userInfo.UserPass,userInfo.Email,userInfo.RegTime); } //读取模板文件的路径 string filePath = context.Request.MapPath("UserInfoList.html"); //读取其中全部的字节 string fileCotent = File.ReadAllText(filePath); //替换字符串 fileCotent = fileCotent.Replace("@tbody",sb.ToString()); context.Response.Write(fileCotent); } public bool IsReusable { get { return false; } } } }