Exemple #1
0
        /// <summary>
        /// select
        /// </summary>
        /// <returns></returns>
        public IEnumerable <T> Select()
        {
            PropertyInfo[] Fields = typeof(T).GetProperties();

            using (MySqlConnection conn = connect())
            {
                using (MySqlCommand comm = new MySqlCommand(QueryStirng.ToString(), conn))
                {
                    conn.Open();
                    if (Paras != null && Paras.Count() > 0)
                    {
                        comm.Parameters.AddRange(Paras.ToArray());
                    }
                    using (DbDataReader reader = comm.ExecuteReader())
                    {
                        T model = default(T);
                        while (reader.Read())
                        {
                            model = (T)Activator.CreateInstance(typeof(T));
                            foreach (PropertyInfo Field in Fields)
                            {
                                if (reader[Field.Name] == DBNull.Value)
                                {
                                    Field.SetValue(model, null, null);
                                }
                                else
                                {
                                    var type  = Type.GetType(Field.PropertyType.FullName);
                                    var value = Helper.ConvertHelper.Convert(reader[Field.Name], type);
                                    Field.SetValue(model, value, null);
                                }
                            }
                            yield return(model);
                        }
                    }
                }
            }
        }