<1>
/// <summary>
/// DAL层
/// </summary>
public class UserInfoService
{
/// <summary>
/// 根据用户名查询数据,将查询出来的数据转换成1个list (将
数据库里的数据表T_User映照到实体类UserInfo)
/// </summary>
/// <param name="userName">用户名</param>
/// <returns></returns>
public static List<UserInfo> SelectDataToEntity(string userName)
{
List<UserInfo> list = SqlHelper.SelectDataToList<UserInfo>("select * from T_User where UserName=@username",
new SqlParameter("username", userName));
return list;
}
/// <summary>
/// 更新数据到
数据库
/// </summary>
/// <param name="u">1个实体类对象</param>
/// <returns></returns>
public static int UpdateDate(UserInfo u)
{
string sql = "update T_User set UserName=@userName,Password=@pwd,Email=@email,Age=@age,Gender=@gender,State=@state,VCode=@VCode where UserId=@uid";
return SqlHelper.ExecuteNonQuery(sql,
new SqlParameter("@userName",u.UserName),
new SqlParameter("@pwd",u.Password),
new SqlParameter("@email",u.Email),
new SqlParameter("@age",u.Age),
new SqlParameter("@gender",u.Gender),
new SqlParameter("@state",u.State),
new SqlParameter("@vcode",u.VCode),
new SqlParameter("@uid", u.UserId)
);
}
}
}
<2>
/// <summary>
/// BLL层
/// </summary>
public class ResetPwd : IHttpHandler
{
/// <summary>
/// 根据用户名查询出当前这条数据(其实这条数据就是1个对象)
/// </summary>
/// <param name="context">用户名</param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string username = context.Request["UserName"];
string pwd = context.Request["newPassword"];
List<UserInfo> list = UserInfoService.SelectDataToEntity(username);
//由于查询出来的是1个list,所以这里获得这个list的第1条数据(有且只能有1条)
UserInfo u = list.Single();
u.Password = pwd; //这里你可以将你要更新的字段赋新值。这里我仅仅是更新密码,固然你也能够跟新其他的。
//u.Email = newEmail; 可以跟新邮箱(示例)
//u.Age = newAge; 可以跟新年龄(示例)
//调用DAL层中的 DataUpdate()方法,将这个UserInfo对象u作为参数传递过去,来将数据更新到
数据库。
UserInfoService.UpdateDate(u);
}
Model
UserInfo类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Model
{
public class UserInfo
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int Age { get; set; }
public int Gender { get; set; }
public int State { get; set; }
public Guid VCode { get; set; }
}
}