爱老婆的猪

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 #region 把DataRow转化成对象
        /// <summary>
        /// 把DataRow转化成对象
        /// </summary>
        /// <param name="dr">dr</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static object DataRowToObj(System.Data.DataRow dr, object obj)
        {
            try
            {
                if (dr != null && obj != null)
                {
                    Type t = obj.GetType();
                    PropertyInfo[] f = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                    string tableName = t.Name;
                    foreach (PropertyInfo pi in f)
                    {
                        string field = pi.Name;
                        if (dr.Table.Columns.Contains(field) && dr[field] != DBNull.Value)
                            pi.SetValue(obj, dr[field], null);
                    }
                }
            }
            catch (Exception ex)
            {
                new Exception(ex.ToString());
                return null;
            }
            return obj;
        }
        #endregion
posted on 2013-04-16 11:37  bloodyboy  阅读(227)  评论(0编辑  收藏  举报