예제 #1
0
        public static T MapSingle <T>(DataTable source) where T : class
        {
            T returnValue = null;

            if (source == null || source.Rows.Count == 0)
            {
                return(returnValue);
            }

            PropertyInfo[] properties = typeof(T).GetProperties();

            DataRow row = source.Rows[0];

            returnValue = (T)Activator.CreateInstance(typeof(T));

            #region set properties
            foreach (PropertyInfo prop in properties.Where(pi => pi.CanWrite))
            {
                string sourceColumn = MappingHelpers.GetSourceColumn(prop);
                if (sourceColumn != MappingHelpers.DO_NOT_MAP)
                {
                    int col = source.Columns.IndexOf(sourceColumn);
                    if (col < 0 || row.IsNull(col))
                    {
                        continue;
                    }

                    MapType <T>(returnValue, row[col], prop);
                }
            }
            #endregion set properties

            return(returnValue);
        }
예제 #2
0
        public static T MapSingle <T>(SqlDataReader source) where T : class
        {
            T returnValue = null;

            if (source == null || !source.HasRows)
            {
                return(returnValue);
            }

            IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();

            if (properties != null && properties.Count() > 0)
            {
                properties = properties.Where(pi => pi.CanWrite);
            }

            if (source.Read())
            {
                returnValue = (T)Activator.CreateInstance(typeof(T));

                #region set properties
                foreach (PropertyInfo prop in properties)
                {
                    try
                    {
                        string sourceColumn = MappingHelpers.GetSourceColumn(prop);
                        int    col          = source.GetOrdinal(sourceColumn);
                        if (col < 0 || source.IsDBNull(col))
                        {
                            continue;
                        }

                        MapType <T>(returnValue, source.GetValue(col), prop);
                    }
                    catch (Exception) { }
                }
                #endregion set properties
            }

            return(returnValue);
        }
예제 #3
0
 private SqlMappingInfo(PropertyInfo prop)
 {
     ColumnNumber = -1;
     SourceColumn = MappingHelpers.GetSourceColumn(prop);
 }