一、常用技术概括及介绍
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();
}
}
}
}
}
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace CZBK.ItcastProject.DAL
{
public class UserInfoDal
{
/// <summary>
/// 获取用户列表
/// </summary>
/// <returns></returns>
public List<UserInfo> GetList()
{
string sql = "select * from UserInfo";
DataTable da = SqlHelper.GetDataTable(sql, CommandType.Text);
List<UserInfo> list = null;
if (da.Rows.Count > )
{
list = new List<UserInfo>();
UserInfo userInfo = null;
foreach (DataRow row in da.Rows)
{
userInfo = new UserInfo();
LoadEntity(userInfo, row);
list.Add(userInfo);
}
}
return list;
}
/// <summary>
/// 添加用户信息
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
public int AddUserInfo(UserInfo userInfo)
{
string sql = "insert into UserInfo(UserName,UserPass,RegTime,Email) values(@UserName,@UserPass,@RegTime,@Email)";
SqlParameter[] pars = {
new SqlParameter("@UserName",SqlDbType.NVarChar,),
new SqlParameter("@UserPass",SqlDbType.NVarChar,),
new SqlParameter("@RegTime",SqlDbType.DateTime),
new SqlParameter("@Email",SqlDbType.NVarChar,)
};
pars[].Value = userInfo.UserName;
pars[].Value = userInfo.UserPass;
pars[].Value = userInfo.RegTime;
pars[].Value = userInfo.Email;
return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars);
} /// <summary>
/// 根据ID删除用户的信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int DeleteUserInfo(int id)
{
string sql = "delete from UserInfo where ID=@ID";
SqlParameter[] pars = {
new SqlParameter("@ID",SqlDbType.Int)
};
pars[].Value = id;
return SqlHelper.ExecuteNonquery(sql,CommandType.Text,pars);
} /// <summary>
/// 修改用户信息
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
public int EditUserInfo(UserInfo userInfo)
{
string sql = "update UserInfo set UserName=@UserName,UserPass=@UserPass,RegTime=@RegTime,Email=@Email where ID=@ID";
SqlParameter[] pars = {
new SqlParameter("@UserName",SqlDbType.NVarChar,),
new SqlParameter("@UserPass",SqlDbType.NVarChar,),
new SqlParameter("@RegTime",SqlDbType.DateTime),
new SqlParameter("@Email",SqlDbType.NVarChar,),
new SqlParameter("@ID",SqlDbType.Int)
};
pars[].Value = userInfo.UserName;
pars[].Value = userInfo.UserPass;
pars[].Value = userInfo.RegTime;
pars[].Value = userInfo.Email;
pars[].Value = userInfo.Id;
return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars);
} /// <summary>
/// 根据用户的编号,获取用户的信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public UserInfo GetUserInfo(int id)
{
string sql = "select * from UserInfo where ID=@ID";
SqlParameter[] pars = {
new SqlParameter("@ID",SqlDbType.Int)
};
pars[].Value = id;
DataTable da=SqlHelper.GetDataTable(sql, CommandType.Text, pars);
UserInfo userInfo = null;
if (da.Rows.Count > )
{
userInfo = new UserInfo();
LoadEntity(userInfo, da.Rows[]);
}
return userInfo;
} private void LoadEntity(UserInfo userInfo, DataRow row)
{
userInfo.UserName = row["UserName"] != DBNull.Value ? row["UserName"].ToString() : string.Empty;
userInfo.UserPass = row["UserPass"] != DBNull.Value ? row["UserPass"].ToString() : string.Empty;
userInfo.Email = row["Email"] != DBNull.Value ? row["Email"].ToString() : string.Empty;
userInfo.Id = Convert.ToInt32(row["ID"]);
userInfo.RegTime = Convert.ToDateTime(row["RegTime"]);
}
}
}
具体增删改查部分
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. 业务层中写一个调用对象方法的方法
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CZBK.ItcastProject.DAL;
namespace CZBK.ItcastProject.BLL
{
public class UserInfoService
{
UserInfoDal UserInfoDal = new UserInfoDal();
/// <summary>
/// 返回数据列表
/// </summary>
/// <returns></returns>
public List<UserInfo> GetList()
{
return UserInfoDal.GetList();
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
public bool AddUserInfo(UserInfo userInfo)
{
return UserInfoDal.AddUserInfo(userInfo)>;
}
/// <summary>
/// 根据ID删除用户的信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool DeleteUserInfo(int id)
{
return UserInfoDal.DeleteUserInfo(id) > ;
}
/// <summary>
/// 修改用户信息
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
public bool EditUserInfo(UserInfo userInfo)
{
return UserInfoDal.EditUserInfo(userInfo) > ;
}
/// <summary>
/// 根据用户的编号,获取用户的信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public UserInfo GetUserInfo(int id)
{
return UserInfoDal.GetUserInfo(id);
} }
}
UserInfoService
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>
UserInfoList.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;
}
}
}
}
UserInfoList.ashx