http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 6931|回复: 0

[教程] 【分布式系统框架教程】-添加一条数据

[复制链接]
发表于 2014-11-28 08:31:57 | 显示全部楼层 |阅读模式
本帖最后由 songwenqi 于 2014-11-28 09:06 编辑

【分布式系统框架教程】-添加一条数据

导读部分



1.【分布式系统框架】教程目录帖http://www.sufeinet.com/thread-10624-1-1.html

教程部分


上几篇帖子已经提到过,各种准备工作,首先需要配置数据库连接字符串,这里就不再贴代码了,LoginUserInfo.cs和UserInfo.cs两个model类也不在贴了,可以到查询两个表之间链接表的数据去看一下这两个类,这篇主要讲一下如何添加数据,下面贴出LoginUserDAL.cs和UserDAL.cs的添加方法,我只贴相关代码,具体例子,喜欢的亲们可以去购买我们的分布式框架结合教程,就一目了然了。
[C#] 纯文本查看 复制代码
namespace DistributedDAL.User
{
    public class LoginUserDAL : BaseDAL<LoginUserInfo>
    {
        /// <summary>
        /// 数据库连接字符串名称
        /// </summary>
        protected override string ConnName
        {
            get { return DbConfig.LoginUserConnection; }
        }
        /// <summary>
        /// 组织对象
        /// </summary>
        /// <param name="reader">Reader对象</param>
        /// <param name="fields">字段集合</param>
        /// <returns></returns>
        protected override LoginUserInfo FillModelFromReader(DbDataReader reader, params string[] fields)
        {
            return ModelFromReader(reader, fields);
        }

        /// <summary>
        /// 组织对象
        /// </summary>
        /// <param name="reader">Reader对象</param>
        /// <param name="fields">字段集合</param>
        /// <returns></returns>
        protected LoginUserInfo ModelFromReader(DbDataReader reader, params string[] fields)
        {
            var info = new LoginUserInfo();
            if (UtilDAL.HasFields("ID", fields)) info.ID = (int)reader["ID"];
            if (UtilDAL.HasFields("UserName", fields)) info.UserName = reader["UserName"].ToString();
            if (UtilDAL.HasFields("UserPwd", fields)) info.UserPwd = reader["UserPwd"].ToString();
            if (UtilDAL.HasFields("UserStatus", fields)) info.UserStatus = (UserStatus)(int)reader["UserStatus"];
            if (UtilDAL.HasFields("LoginIp", fields)) info.LoginIp = reader["LoginIp"].ToString();
            if (UtilDAL.HasFields("LoginType", fields)) info.LoginType = (LoginType)(int)reader["LoginType"];

            return info;
        }
/// <summary>
        /// 添加
        /// </summary>
        /// <param name="info">LoginUserInfo对象</param>
        /// <returns></returns>
        public int AddLoginUserInfo(LoginUserInfo info)
        {
            var sql = "INSERT INTO [LoginUser]([ID],[UserName],[UserPwd],[UserStatus],[LoginIp],[LoginType])"
             + "VALUES(@Id,@UserName,@UserPwd,@UserStatus,@LoginIp,@LoginType)";
            return Execute(sql, UtilDAL.CreateParameter("@Id", info.ID),
                UtilDAL.CreateParameter("@UserName", info.UserName),
                UtilDAL.CreateParameter("@UserPwd", info.UserPwd),
                UtilDAL.CreateParameter("@UserStatus", info.UserStatus),
                UtilDAL.CreateParameter("@LoginIp", info.LoginIp),
                UtilDAL.CreateParameter("@LoginType", info.LoginType));
        }
}
UserDAL.cs:
[C#] 纯文本查看 复制代码
 public class UserDAL:BaseDAL<UserInfo>
    {
        /// <summary>
        /// 数据库连接字符串名称
        /// </summary>
       protected override string ConnName
       {
           get { return DbConfig.UserConnection; }
       }
       /// <summary>
       /// 组织对象
       /// </summary>
       /// <param name="reader">Reader对象</param>
       /// <param name="fields">字段集合</param>
       /// <returns></returns>
       protected override UserInfo FillModelFromReader(DbDataReader reader, params string[] fields)
       {
           return ModelFromReader(reader, fields);
       }
 /// <summary>
       /// 添加
       /// </summary>
       /// <param name="user">UserInfo对象</param>
       /// <returns>返回当前插入的对象ID</returns>
       public int AddUserInfo(UserInfo user)
       {
           var sql = "INSERT INTO [User] ([Name] ,[Sex] ,[Phone] ,[ProvinceId] ,[CityId] ,[ConstellationId] ,[DegreeId] ,[NationId] ,[ZodiacId] ) "
               + "VALUES (@Name,@Sex,@Phone,@ProvinceId,@CityId,@ConstellationId,@DegreeId,@NationId,@ZodiacId);SELECT  @@IDENTITY as ID";
           object ret=GetScalar(sql,UtilDAL.CreateParameter("@Name",user.Name),
               UtilDAL.CreateParameter("@Sex",user.Sex),
               UtilDAL.CreateParameter("@Phone",user.Phone),
               UtilDAL.CreateParameter("@ProvinceId",user.ProvinceId),
               UtilDAL.CreateParameter("@CityId",user.CityId),
               UtilDAL.CreateParameter("@ConstellationId",user.ConstellationId),
               UtilDAL.CreateParameter("@DegreeId",user.DegreeId),
               UtilDAL.CreateParameter("@NationId",user.NationId),
               UtilDAL.CreateParameter("@ZodiacId",user.ZodiacId));
           return Convert.ToInt32(ret);
       }
}
以下是bll层代码
[C#] 纯文本查看 复制代码
public class LoginUserBLL
    {
      LoginUserDAL dal = new LoginUserDAL();
      /// <summary>
      /// 添加一条记录
      /// </summary>
      /// <param name="info"></param>
      /// <returns></returns>
      public int AddLoginUserInfo(LoginUserInfo info)
      {
          return dal.AddLoginUserInfo(info);
      }
}

[C#] 纯文本查看 复制代码
public class UserBLL
    {
        UserDAL dal=new UserDAL();
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="user">UserInfo对象</param>
        /// <returns>返回当前插入对象ID</returns>
        public int AddUserInfo(UserInfo user)
        {
            return dal.AddUserInfo(user);
        }
}

好了,准备工作已经完成,下面是web端的代码了:
[C#] 纯文本查看 复制代码
  UserBLL userbll=new UserBLL();      LoginUserBLL loginbll=new LoginUserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSelect();//这个以后会专门有一篇将如何使用静态集合
            }
        }

protected void btnOk_Click(object sender, EventArgs e)
        {

            if (Page.IsValid)
            {
                Add();
                
            }
        }
public void Add()
        {
            if (Request["id"] == null)
            {
                //从页面输入信息组织UserInfo对象
                UserInfo user = GetUser();
                //添加成功返回当前对象的唯一标识ID
                int userId = userbll.AddUserInfo(user);
                if (userId > 0)
                {
                    //从页面输入信息组织LoginUserInfo对象(传递唯一标识ID)
                    LoginUserInfo loginuser = GetLoginUser(userId);
                    //添加LoginUserInfo对象
                    loginbll.AddLoginUserInfo(loginuser);
                    Response.Redirect("list.aspx");
                }
            }
        }
/// <summary>
        /// 获取UserInfo对象 通过页面 此时返回的对象ID未赋值  修改操作时需赋ID值
        /// </summary>
        /// <returns></returns>
        public UserInfo GetUser()
        {
            string name = txtUserName.Value;
            string phone = txtPhone.Value.Trim(); ;
            string sex = ddlGender.Value;
            string zodiac = ddlZodiac.Value;
            string constellation = ddlConstellation.Value;
            string degree = ddlDegree.Value;
            string nation = ddlNation.Value;
            string provice = ddlProv.Value;
            string city = ddlCity.Value;
            UserInfo user = new UserInfo
            {
                Name = name,
                Phone = phone,
                Sex = InputHelper.GetInputInt(sex),
                ZodiacId = InputHelper.GetInputInt(zodiac),
                ConstellationId = InputHelper.GetInputInt(constellation),
                DegreeId = InputHelper.GetInputInt(degree),
                NationId = InputHelper.GetInputInt(nation),
                ProvinceId = InputHelper.GetInputInt(provice),
                CityId = InputHelper.GetInputInt(city)
            };
            return user;
        }
        /// <summary>
        /// 获取LoginUserInfo对象
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public LoginUserInfo GetLoginUser(int userId)
        {
            string loginUser = txtLoginUserName.Value;
            string userStatus = ddlUserStatus.Value;
            string pwd = txtPwd.Value;
            string loginType = ddlLoginType.Value;
            string loginIP = Request.UserHostAddress;
            LoginUserInfo loginuser = new LoginUserInfo
            {
                ID = userId,
                UserName = loginUser,
                UserPwd = pwd,
                LoginIp = loginIP,
                UserStatus = (UserStatus)int.Parse(userStatus),
                LoginType = (LoginType)int.Parse(loginType)
            };
            return loginuser;
        }

效果如下:
QQ截图20141128083236.png
QQ截图20141128083315.png




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-15 17:28

© 2014-2021

快速回复 返回顶部 返回列表