码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-15 00:30:34      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

<1>

    /// <summary>
    /// DAL层
    /// </summary>
    public class UserInfoService
    {
        /// <summary>
        /// 根据用户名查询数据,将查询出来的数据转换成一个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">一个实体类对象</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>
        /// 根据用户名查询出当前这条数据(其实这条数据就是一个对象) 
        /// </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);

            //因为查询出来的是一个list,所以这里取得这个list的第一条数据(有且只能有一条)
            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; }
    }
}


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

标签:

原文地址:http://blog.csdn.net/fanbin168/article/details/42722479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!