示例#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 decimal?GetNullableDecimal(object expression)
 {
     if (expression == null || Convert.IsDBNull(expression))
     {
         return(null);
     }
     else
     {
         if (expression.GetType() == typeof(string))
         {
             if (string.IsNullOrEmpty((string)expression))
             {
                 return(null);
             }
         }
         if (!OleDb.IsNumeric(expression))
         {
             throw new Exception(String.Format("{0} no es numérica", expression));
         }
         return(Convert.ToDecimal(expression));
     }
 }
示例#3
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