Ejemplo n.º 1
0
        /// <summary>
        ///  DataRow转换为指定类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <returns></returns>
        public static T ToT <T>(this DataRow row)
        {
            if (row == null)
            {
                return(default(T));
            }

            //值类型直接返回第一列
            Type tp = typeof(T);

            if (ObjectExtession.IsValueType(tp))
            {
                return((T)ObjectExtession.DbChangeType(row[0], tp));
            }

            T tReturn = Activator.CreateInstance <T>();

            //属性列表
            var properties = tp.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo property in properties)
            {
                if (row.Table.Columns.Contains(property.Name))
                {
                    object objValue = row[property.Name];

                    //忽略空值,忽略只读属性
                    if (!ObjectExtession.IsNullOrDbNull(objValue) && property.CanWrite)
                    {
                        property.SetValue(tReturn, ObjectExtession.DbChangeType(objValue, property.PropertyType));
                    }
                }
            }
            return(tReturn);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取第一行数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T SqlQueryFirst <T>(this DbContext db, string sql, params DbParameter[] parameters)
        //where T : class,new()
        {
            var conn = db.Database.GetDbConnection();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                using (var reader = PreCommandReader(db.Database, sql, parameters))
                {
                    T   model  = Activator.CreateInstance <T>();
                    var type   = typeof(T);
                    var propts = type.GetProperties();
                    if (reader.Read())
                    {
                        if (ObjectExtession.IsValueType(type))//增加对可空类型和string datetime兼容
                        {
                            return((T)ObjectExtession.DbChangeType(reader[0], type));
                        }
                        else
                        {
                            foreach (var propt in propts)
                            {
                                object value = reader[propt.Name];
                                propt.SetValue(model, value == DBNull.Value ? null : value);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("SqlQueryFirst未读取到任何数据");
                    }

                    return(model);
                }
            }
            finally
            {
                conn.Close();
            }
        }