c# 获取更新数据库信息

通过ObjectHelper类对数据库信息进行拷贝和赋值


//新增

public static int CreateUser(JsonUser user){

  using (TeleMedicineEntities context = new TeleMedicineEntities())
            {

            User u = context.User.Create();

             ObjectHelper.CopyToObject<User>(user,ref u);

            c= context.User.Add(c);

            context.SavaChanges();

          return c.Id;

         }

}

  //查询

public static JsonUser RetrieveUser(string accountName, string password, int role)

        {
            using (TeleMedicineEntities context = new TeleMedicineEntities())
            {
                var user = (from u in context.User
                            where u.EmployeeNo == accountName  && u.Role == role
                            select u).FirstOrDefault();

                if (user == null)
                    return null;
                else
                {
                 
                    JsonUser ju = new JsonUser();
                    ObjectHelper.CopyToObject<JsonUser>(user, ref ju);
                    return ju;
              
                }
            }

        }


     /// <summary>
        /// 修改用户
        /// </summary>
        /// <returns></returns>
        public static bool UpdateUsers(JsonUser user)
        {
            bool result = false;

            using (TeleMedicineEntities context = new TeleMedicineEntities())
            {
                User c = context.User.Find(user.Id);
                if (c != null)
                {
                    ObjectHelper.CopyToObject<User>(user, ref c);

                    result = (context.SaveChanges() == 1);
                }

            }

            return result;
        }


---------------------------------------------------------------------------------------------------------

   class ObjectHelper
    {
        /// <summary>
        /// Copy an object to destination object, only matching fields will be copied
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceObject">An object with matching fields of the destination object</param>
        /// <param name="destObject">Destination object, must already be created</param>
        public static void CopyToObject<T>(object sourceObject, ref T destObject)
        {
            //    If either the source, or destination is null, return
            if (sourceObject == null || destObject == null)
                return;

            //    Get the type of each object
            Type sourceType = sourceObject.GetType();
            Type targetType = destObject.GetType();

            //    Loop through the source properties
            foreach (PropertyInfo p in sourceType.GetProperties())
            {
                //    Get the matching property in the destination object
                PropertyInfo targetObj = targetType.GetProperty(p.Name);
                //    If there is none, skip
                if (targetObj == null)
                    continue;

                //    Set the value in the destination
                targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
            }

            return;
        }
    }


}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值