Exemplo n.º 1
0
        /// <summary>
        /// 根據条件表达式找出需要的数据并返回
        /// </summary>
        /// <param name="filterExpresstion"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public DataTableStd SelectByWhere(string filterExpresstion, string sort)
        {
            DataRow[]    drs = this.Value.Select(filterExpresstion, sort);
            DataTableStd rtn = this.CloneStd();

            foreach (DataRow dr in drs)
            {
                foreach (string colname in this.ColumnNames)
                {
                    rtn.SetNewRowValue(dr[colname], colname);
                }
                rtn.AddNewRow();
            }
            return(rtn);
        }
        /// <summary>
        /// 转化成标准类型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DataTableStd ParseStd(object o)
        {
            if (o == null)
            {
                return(null);
            }
            else if (o is DbDataReader)
            {
                var ddr = (DbDataReader)o;
                var rtn = new DataTableStd();

                if (!ddr.CanGetColumnSchema())
                {
                    for (var i = 0; i < ddr.FieldCount; i++)
                    {
                        rtn.AddColumn(new ColumnP()
                        {
                            ColumnName = ddr.GetName(i)
                        });
                    }

                    while (ddr.Read())
                    {
                        rtn.NewRow();
                        foreach (var col in rtn.schema)
                        {
                            var val = ddr[col.ColumnName];
                            rtn.SetNewRowValue(ConvertDBValue2Object(val), col.ColumnName);
                        }
                        rtn.AddNewRow();
                    }
                }
                else
                {
                    var cschema = ddr.GetColumnSchema();
                    for (int i = 0; i < ddr.FieldCount; i++)
                    {
                        rtn.AddColumn(new ColumnP()
                        {
                            ColumnName      = cschema[i].ColumnName,
                            DataType        = cschema[i].DataType.FullName,
                            IsAllowNull     = cschema[i].AllowDBNull == null ? false : cschema[i].AllowDBNull.Value,
                            IsAutoIncrement = cschema[i].IsAutoIncrement == null ? false : cschema[i].IsAutoIncrement.Value,
                            IsPK            = cschema[i].IsKey == null ? false : cschema[i].IsKey.Value,
                            Length          = cschema[i].ColumnSize == null ? -1 : cschema[i].ColumnSize.Value
                        });
                    }

                    while (ddr.Read())
                    {
                        rtn.NewRow();
                        foreach (var col in rtn.ColumnNames)
                        {
                            rtn.SetNewRowValue(ConvertDBValue2Object(ddr[col]), col);
                        }

                        rtn.AddNewRow();
                    }
                }


                return(rtn);
            }
            else if (o.GetType() != Type.GetType("System.Data.DataTable"))
            {
                return(null);
            }
            else if (o is DataTableStd)
            {
                return((DataTableStd)o);
            }
            else if (o is IEnumerable <object> )
            {
                var rtn  = new DataTableStd();
                var list = ((IEnumerable <object>)o).Select(d => (FrameDLRObject)FrameDLRObject.CreateInstance(d, Constants.FrameDLRFlags.SensitiveCase));
                if (list.Count() > 0)
                {
                    var first = list.First();
                    foreach (var item in first.Items)
                    {
                        rtn.AddColumn(new ColumnP()
                        {
                            ColumnName = item.Key
                        });
                    }
                    foreach (var item in list)
                    {
                        rtn.NewRow();
                        foreach (var key in item.Keys)
                        {
                            rtn.SetNewRowValue(item.GetValue(key), key);
                        }
                        rtn.AddNewRow();
                    }
                }
                return(rtn);
            }
            else
            {
                return(null);
            }
        }