コード例 #1
0
        public virtual DataTable GetFillTable(TableMapping tableMapping, IList list)
        {
            DataTable dataTable = this.GetTableSchema(tableMapping);

            object[] values = new object[dataTable.Columns.Count];
            dataTable.BeginInit();
            dataTable.BeginLoadData();
            foreach (object obj in list)
            {
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    var ColumnName = dataTable.Columns[i].ColumnName;
                    var col        = tableMapping.GetColumnMappingByColumn(ColumnName);
                    if (col != null)
                    {
                        values[i] = col.GetValue(obj);
                    }
                }

                dataTable.LoadDataRow(values, false);
            }
            dataTable.EndLoadData();
            dataTable.EndInit();
            return(dataTable);
        }
コード例 #2
0
        public void Load(Type objectType, IList list, IDataReader dr)
        {
            if (objectType.IsPrimitive || (objectType == typeof(string)))
            {
                while (dr.Read())
                {
                    object val = dr[0];
                    if (val == DBNull.Value)
                    {
                        if (objectType == typeof(string))
                        {
                            list.Add(null);
                        }
                        else
                        {
                            list.Add(Activator.CreateInstance(objectType));
                        }
                    }
                    else
                    {
                        list.Add(Convert.ChangeType(val, objectType));
                    }
                }
            }
            else
            {
                int           fieldcount = dr.FieldCount;
                List <string> fieldnames = new List <string>();
                for (int i = 0; i < fieldcount; i++)
                {
                    fieldnames.Add(dr.GetName(i));
                }

                TableMapping tableMapping = MappingService.Instance.GetTableMapping(objectType, false);

                while (dr.Read())
                {
                    object           obj    = Activator.CreateInstance(objectType);
                    IDynamicProperty dynObj = obj as IDynamicProperty;

                    foreach (string fieldname in fieldnames)
                    {
                        object val = dr[fieldname];

                        ColumnMapping column = null;
                        if (tableMapping != null)
                        {
                            column = tableMapping.GetColumnMappingByColumn(fieldname);
                        }
                        if (column != null)
                        {
                            column.SetValue(obj, val);
                        }
                        else if (val != DBNull.Value)
                        {
                            PropertyInfo prop = objectType.GetProperty(fieldname);
                            if (prop == null && dynObj != null)
                            {
                                dynObj.SetData(fieldname, val);
                            }
                            else if (prop != null)
                            {
                                if (val.GetType() != prop.PropertyType)
                                {
                                    val = Convert.ChangeType(val, prop.PropertyType);
                                }
                                prop.SetValue(obj, val, null);
                            }
                        }
                    }
                    list.Add(obj);
                }
            }
        }