示例#1
0
        /// <summary>
        /// Uses the SQL Generator to dynamically create a SELECT statement based on
        /// the properties of the model referenced and returns an array of the
        /// referenced model populated from the API.
        /// </summary>
        public static IEnumerable <T> LoadModel <T>(SqlGenerator gen = null)
        {
            Type     t = typeof(T);
            List <T> o = Activator.CreateInstance <List <T> >();

            DatabaseModelBindings bindings = t.GetDatabaseBindings();

            if (gen == null)
            {
                gen = new SqlGenerator(SqlGenerator.SqlTypes.Select, bindings.TableName);
                gen.SelectFromModel <T>();
            }

            SqlDataReader dr = Adocls.FetchDataReader(gen, bindings.DatabaseName);

            if (dr != null)
            {
                while (dr.Read())
                {
                    T newo = Activator.CreateInstance <T>();

                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        string name = dr.GetName(i);

                        PropertyInfo pi = t.GetProperty(name);

                        if (dr[i] != System.DBNull.Value)
                        {
                            if (pi != null && pi.CanWrite)
                            {
                                object val  = dr[i];
                                Type   newT = val.GetType();

                                if (pi.PropertyType.UnderlyingSystemType == typeof(bool))
                                {
                                    if (newT == typeof(bool))
                                    {
                                        pi.SetValue(newo, val);
                                    }
                                    else if (newT == typeof(byte))
                                    {
                                        pi.SetValue(newo, (byte)val == 1);
                                    }
                                }
                                else if (pi.PropertyType.UnderlyingSystemType == typeof(string))
                                {
                                    // We must excape the # when it is in text
                                    pi.SetValue(newo, val.ToString().Replace("#", "\\#"));
                                }
                                else
                                {
                                    pi.SetValue(newo, val);
                                }
                            }
                        }
                    }
                    o.Add(newo);
                }
            }
            else
            {
                Console.WriteLine();
            }

            return(o);
        }
示例#2
0
 public SqlDataReader GetDataReader(string connectionName = "CountyDatabase")
 {
     return(Adocls.FetchDataReader(this, connectionName));
 }