利用对象来更新数据 UpdateData(object o)

<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; }
}
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » 利用对象来更新数据 UpdateData(object o)

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡