コード例 #1
0
 /// <summary>
 /// 转化成标准类型
 /// </summary>
 /// <param name="o"></param>
 /// <returns></returns>
 public static DataTableStd ParseStd(DataTable o)
 {
     if (o == null)
     {
         return(null);
     }
     else
     {
         DataTableStd dts = new DataTableStd();
         foreach (DataColumn c in o.Columns)
         {
             dts.AddColumn(c, o.PrimaryKey.Contains(c));
         }
         return(dts);
     }
 }
コード例 #2
0
        /// <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);
            }
        }