예제 #1
0
        } // public static object QueryScalar

        public static List <T> TableDataReader <T>()
        {
            OleDbCommand command = new OleDbCommand();

            command.Connection = new OleDbConnection(connStr);
            try
            {
                List <T>       result = new List <T>();
                object         instance;
                PropertyInfo[] props;
                string         tableName;
                tableName = OleDb.TableName(typeof(T));

                instance = (T)Activator.CreateInstance(typeof(T));
                props    = typeof(T).GetProperties();

                if (command.Connection.State == ConnectionState.Closed)
                {
                    command.Connection.Open();
                }
                command.CommandType = CommandType.Text;
                command.CommandText = OleDb.SelectStatement(tableName);
                command.Parameters.Clear();

                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    instance = (T)Activator.CreateInstance(typeof(T));
                    foreach (PropertyInfo pi in props)
                    {
                        object val = reader[pi.Name];
                        if (Convert.IsDBNull(val))
                        {
                            val = null;
                        }
                        pi.SetValue(instance, val, null);
                    }
                    result.Add((T)instance);
                }
                return(result);
            }
            finally
            {
                command.Connection.Close();
                command.Dispose();
            }
        } // end void
예제 #2
0
        public static List <T> TableData <T>()
        {
            OleDbCommand command = new OleDbCommand();

            command.Connection = new OleDbConnection(connStr);
            try
            {
                List <T> result = new List <T>();
                object   instance;
                string   tableName;
                tableName = OleDb.TableName(typeof(T));

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

                DataTable dt = OleDb.Select(tableName);
                foreach (DataRow dr in dt.Rows)
                {
                    //instance = (T)Activator.CreateInstance(typeof(T));
                    foreach (DataColumn dc in dt.Columns)
                    {
                        object val = dr[dc.ColumnName];
                        if (Convert.IsDBNull(val))
                        {
                            val = null;
                        }
                        PropertyInfo pi = instance.GetType().GetProperty(dc.ColumnName);
                        pi.SetValue(instance, val, null);
                    }
                    result.Add((T)instance);
                }
                return(result);
            }
            finally
            {
                command.Connection.Close();
                command.Dispose();
            }
        } // public static object QueryScalar