private void toolAddColumns_Click(Object sender, EventArgs e) { var dc = CurrentTable.CreateColumn(); //CurrentTable.Columns.Add(dc); var id = CurrentTable.Columns.Count + 1; dc.ColumnName = "Column" + id; dc.Description = "字段" + id; var dr = AddField.CreateForm(dc, true).ShowDialog(); if (dr != DialogResult.Cancel) { CurrentTable.Columns.Add(dc); BandingDGV(); } }
/// <summary>复制数据表到另一个数据表,复制所有数据列、索引和关系</summary> /// <param name="src"></param> /// <param name="des"></param> /// <param name="resetColumnID">是否重置列ID</param> /// <returns></returns> public static IDataTable CopyAllFrom(this IDataTable src, IDataTable des, Boolean resetColumnID = false) { src.CopyFrom(des); src.Columns.AddRange(des.Columns.Select(i => src.CreateColumn().CopyFrom(i))); src.Indexes.AddRange(des.Indexes.Select(i => src.CreateIndex().CopyFrom(i))); src.Relations.AddRange(des.Relations.Select(i => src.CreateRelation().CopyFrom(i))); // 重载ID //if (resetColumnID) src.Columns.ForEach((it, i) => it.ID = i + 1); if (resetColumnID) { for (int i = 0; i < src.Columns.Count; i++) { src.Columns[i].ID = i + 1; } } return(src); }
private void btnAddColumn_Click(Object sender, EventArgs e) { IDataTable table = GetSelectedTable(); if (table == null) { return; } IDataColumn dc = table.CreateColumn(); table.Columns.Add(dc); dc.ID = table.Columns.Count; dc.ColumnName = "Column" + dc.ID; dc.Description = "字段" + dc.ID; gv.DataSource = null; gv.DataSource = table.Columns; pgColumn.SelectedObject = dc; }
/// <summary>获取指定表的字段</summary> /// <param name="table"></param> /// <param name="rows"></param> /// <returns></returns> protected virtual List <IDataColumn> GetFields(IDataTable table, DataRow[] rows) { var list = new List <IDataColumn>(); foreach (var dr in rows) { var field = table.CreateColumn(); // 名称 field.ColumnName = GetDataRowValue <String>(dr, _.ColumnName); // 标识、主键 if (TryGetDataRowValue(dr, "AUTOINCREMENT", out Boolean b)) { field.Identity = b; } if (TryGetDataRowValue(dr, "PRIMARY_KEY", out b)) { field.PrimaryKey = b; } // 原始数据类型 field.RawType = GetDataRowValue <String>(dr, "DATA_TYPE", "DATATYPE", "COLUMN_DATA_TYPE"); // 长度 field.Length = GetDataRowValue <Int32>(dr, "CHARACTER_MAXIMUM_LENGTH", "LENGTH", "COLUMN_SIZE"); if (field is XField fi) { // 精度 与 位数 fi.Precision = GetDataRowValue <Int32>(dr, "NUMERIC_PRECISION", "DATETIME_PRECISION", "PRECISION"); fi.Scale = GetDataRowValue <Int32>(dr, "NUMERIC_SCALE", "SCALE"); if (field.Length == 0) { field.Length = fi.Precision; } } // 允许空 if (TryGetDataRowValue(dr, "IS_NULLABLE", out b)) { field.Nullable = b; } else if (TryGetDataRowValue(dr, "IS_NULLABLE", out String str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = "YES".EqualIgnoreCase(str); } } else if (TryGetDataRowValue(dr, "NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = "Y".EqualIgnoreCase(str); } } // 描述 field.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); FixField(field, dr); // 检查是否已正确识别类型 if (field.DataType == null) { WriteLog("无法识别{0}.{1}的类型{2}!", table.TableName, field.ColumnName, field.RawType); } // 非字符串字段,长度没有意义 //else if (field.DataType != typeof(String)) // field.Length = 0; field.Fix(); list.Add(field); } return(list); }
/// <summary>读取</summary> /// <param name="table"></param> /// <param name="reader"></param> /// <returns></returns> public static IDataTable ReadXml(this IDataTable table, XmlReader reader) { // 读属性 if (reader.HasAttributes) { reader.MoveToFirstAttribute(); ReadXml(reader, table); } reader.ReadStartElement(); // 读字段 reader.MoveToElement(); // 有些数据表模型没有字段 if (reader.NodeType == XmlNodeType.Element && reader.Name.EqualIgnoreCase("Table")) { return(table); } while (reader.NodeType != XmlNodeType.EndElement) //while (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "Columns": reader.ReadStartElement(); while (reader.IsStartElement()) { var dc = table.CreateColumn(); var v = reader.GetAttribute("DataType"); if (v != null) { dc.DataType = v.GetTypeEx(false); v = reader.GetAttribute("Length"); if (v != null && Int32.TryParse(v, out var len)) { dc.Length = len; } dc = FixDefaultByType(dc, null); // 清空默认的原始类型,让其从xml读取 dc.RawType = null; } (dc as IXmlSerializable).ReadXml(reader); table.Columns.Add(dc); } reader.ReadEndElement(); // 修正可能的主字段 if (!table.Columns.Any(e => e.Master)) { var f = table.Columns.FirstOrDefault(e => e.Name.EqualIgnoreCase("Name", "Title")); if (f != null) { f.Master = true; } } break; case "Indexes": reader.ReadStartElement(); while (reader.IsStartElement()) { var di = table.CreateIndex(); (di as IXmlSerializable).ReadXml(reader); di.Fix(); table.Indexes.Add(di); } reader.ReadEndElement(); break; case "Relations": reader.ReadStartElement(); reader.Skip(); reader.ReadEndElement(); break; default: // 这里必须处理,否则加载特殊Xml文件时将会导致死循环 reader.Read(); break; } } if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } // 修正 table.Fix(); return(table); }
/// <summary>读取</summary> /// <param name="table"></param> /// <param name="reader"></param> /// <returns></returns> public static IDataTable ReadXml(this IDataTable table, XmlReader reader) { // 读属性 if (reader.HasAttributes) { reader.MoveToFirstAttribute(); ReadXml(reader, table); } reader.ReadStartElement(); // 读字段 reader.MoveToElement(); // 有些数据表模型没有字段 if (reader.NodeType == XmlNodeType.Element && reader.Name.EqualIgnoreCase("Table")) { return(table); } while (reader.NodeType != XmlNodeType.EndElement) //while (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "Columns": reader.ReadStartElement(); var id = 1; while (reader.IsStartElement()) { var dc = table.CreateColumn(); dc.ID = id++; var v = reader.GetAttribute("DataType"); if (v != null) { dc.DataType = v.GetTypeEx(); v = reader.GetAttribute("Length"); var len = 0; if (v != null && Int32.TryParse(v, out len)) { dc.Length = len; } // 含有ID表示是旧的,不需要特殊处理,否则一些默认值会不对 v = reader.GetAttribute("ID"); if (v == null) { dc = Fix(dc, dc); } } (dc as IXmlSerializable).ReadXml(reader); table.Columns.Add(dc); } reader.ReadEndElement(); // 修正可能的主字段 if (!table.Columns.Any(e => e.Master)) { var f = table.Columns.FirstOrDefault(e => e.Name.EqualIgnoreCase("Name", "Title")); if (f != null) { f.Master = true; } } break; case "Indexes": reader.ReadStartElement(); while (reader.IsStartElement()) { var di = table.CreateIndex(); (di as IXmlSerializable).ReadXml(reader); di.Fix(); table.Indexes.Add(di); } reader.ReadEndElement(); break; case "Relations": reader.ReadStartElement(); while (reader.IsStartElement()) { var dr = table.CreateRelation(); (dr as IXmlSerializable).ReadXml(reader); if (table.GetRelation(dr) == null) { table.Relations.Add(dr); } } reader.ReadEndElement(); break; default: // 这里必须处理,否则加载特殊Xml文件时将会导致死循环 reader.Read(); break; } } //if (reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.EndElement) reader.Read(); //reader.ReadEndElement(); if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } return(table); }
/// <summary>获取指定表的字段</summary> /// <param name="table"></param> /// <param name="rows"></param> /// <returns></returns> protected virtual List <IDataColumn> GetFields(IDataTable table, DataRow[] rows) { var list = new List <IDataColumn>(); // 开始序号 Int32 startIndex = 0; foreach (var dr in rows) { var field = table.CreateColumn(); // 序号 Int32 n = 0; if (TryGetDataRowValue <Int32>(dr, _.OrdinalPosition, out n)) { field.ID = n; } else if (TryGetDataRowValue <Int32>(dr, _.ID, out n)) { field.ID = n; } // 如果从0开始,则所有需要同步增加;如果所有字段序号都是0,则按照先后顺序 if (field.ID == 0) { startIndex++; //field.ID = startIndex; } if (startIndex > 0) { field.ID += startIndex; } // 名称 field.ColumnName = GetDataRowValue <String>(dr, _.ColumnName); // 标识、主键 Boolean b; if (TryGetDataRowValue <Boolean>(dr, "AUTOINCREMENT", out b)) { field.Identity = b; } if (TryGetDataRowValue <Boolean>(dr, "PRIMARY_KEY", out b)) { field.PrimaryKey = b; } // 原始数据类型 String str; if (TryGetDataRowValue <String>(dr, "DATA_TYPE", out str)) { field.RawType = str; } else if (TryGetDataRowValue <String>(dr, "DATATYPE", out str)) { field.RawType = str; } else if (TryGetDataRowValue <String>(dr, "COLUMN_DATA_TYPE", out str)) { field.RawType = str; } // 是否Unicode if (Database is DbBase) { field.IsUnicode = (Database as DbBase).IsUnicode(field.RawType); } // 精度 if (TryGetDataRowValue <Int32>(dr, "NUMERIC_PRECISION", out n)) { field.Precision = n; } else if (TryGetDataRowValue <Int32>(dr, "DATETIME_PRECISION", out n)) { field.Precision = n; } else if (TryGetDataRowValue <Int32>(dr, "PRECISION", out n)) { field.Precision = n; } // 位数 if (TryGetDataRowValue <Int32>(dr, "NUMERIC_SCALE", out n)) { field.Scale = n; } else if (TryGetDataRowValue <Int32>(dr, "SCALE", out n)) { field.Scale = n; } // 长度 if (TryGetDataRowValue <Int32>(dr, "CHARACTER_MAXIMUM_LENGTH", out n)) { field.Length = n; } else if (TryGetDataRowValue <Int32>(dr, "LENGTH", out n)) { field.Length = n; } else if (TryGetDataRowValue <Int32>(dr, "COLUMN_SIZE", out n)) { field.Length = n; } else { field.Length = field.Precision; } // 字节数 if (TryGetDataRowValue <Int32>(dr, "CHARACTER_OCTET_LENGTH", out n)) { field.NumOfByte = n; } else { field.NumOfByte = field.Length; } // 允许空 if (TryGetDataRowValue <Boolean>(dr, "IS_NULLABLE", out b)) { field.Nullable = b; } else if (TryGetDataRowValue <String>(dr, "IS_NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = "YES".EqualIgnoreCase(str); } } else if (TryGetDataRowValue <String>(dr, "NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = "Y".EqualIgnoreCase(str); } } // 默认值 field.Default = GetDataRowValue <String>(dr, "COLUMN_DEFAULT"); // 描述 field.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); FixField(field, dr); // 检查是否已正确识别类型 if (field.DataType == null) { WriteLog("无法识别{0}.{1}的类型{2}!", table.TableName, field.ColumnName, field.RawType); } field.Fix(); list.Add(field); } return(list); }
/// <summary>获取指定表的字段</summary> /// <param name="table"></param> /// <param name="rows"></param> /// <returns></returns> protected virtual List<IDataColumn> GetFields(IDataTable table, DataRow[] rows) { var list = new List<IDataColumn>(); // 开始序号 Int32 startIndex = 0; foreach (var dr in rows) { var field = table.CreateColumn(); // 序号 Int32 n = 0; if (TryGetDataRowValue<Int32>(dr, _.OrdinalPosition, out n)) field.ID = n; else if (TryGetDataRowValue<Int32>(dr, _.ID, out n)) field.ID = n; // 如果从0开始,则所有需要同步增加;如果所有字段序号都是0,则按照先后顺序 if (field.ID == 0) { startIndex++; //field.ID = startIndex; } if (startIndex > 0) field.ID += startIndex; // 名称 field.Name = GetDataRowValue<String>(dr, _.ColumnName); // 标识、主键 Boolean b; if (TryGetDataRowValue<Boolean>(dr, "AUTOINCREMENT", out b)) field.Identity = b; if (TryGetDataRowValue<Boolean>(dr, "PRIMARY_KEY", out b)) field.PrimaryKey = b; // 原始数据类型 String str; if (TryGetDataRowValue<String>(dr, "DATA_TYPE", out str)) field.RawType = str; else if (TryGetDataRowValue<String>(dr, "DATATYPE", out str)) field.RawType = str; else if (TryGetDataRowValue<String>(dr, "COLUMN_DATA_TYPE", out str)) field.RawType = str; // 是否Unicode if (Database is DbBase) field.IsUnicode = (Database as DbBase).IsUnicode(field.RawType); // 精度 if (TryGetDataRowValue<Int32>(dr, "NUMERIC_PRECISION", out n)) field.Precision = n; else if (TryGetDataRowValue<Int32>(dr, "DATETIME_PRECISION", out n)) field.Precision = n; else if (TryGetDataRowValue<Int32>(dr, "PRECISION", out n)) field.Precision = n; // 位数 if (TryGetDataRowValue<Int32>(dr, "NUMERIC_SCALE", out n)) field.Scale = n; else if (TryGetDataRowValue<Int32>(dr, "SCALE", out n)) field.Scale = n; // 长度 if (TryGetDataRowValue<Int32>(dr, "CHARACTER_MAXIMUM_LENGTH", out n)) field.Length = n; else if (TryGetDataRowValue<Int32>(dr, "LENGTH", out n)) field.Length = n; else if (TryGetDataRowValue<Int32>(dr, "COLUMN_SIZE", out n)) field.Length = n; else field.Length = field.Precision; // 字节数 if (TryGetDataRowValue<Int32>(dr, "CHARACTER_OCTET_LENGTH", out n)) field.NumOfByte = n; else field.NumOfByte = field.Length; // 允许空 if (TryGetDataRowValue<Boolean>(dr, "IS_NULLABLE", out b)) field.Nullable = b; else if (TryGetDataRowValue<String>(dr, "IS_NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) field.Nullable = String.Equals("YES", str, StringComparison.OrdinalIgnoreCase); } else if (TryGetDataRowValue<String>(dr, "NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) field.Nullable = String.Equals("Y", str, StringComparison.OrdinalIgnoreCase); } // 默认值 field.Default = GetDataRowValue<String>(dr, "COLUMN_DEFAULT"); // 描述 field.Description = GetDataRowValue<String>(dr, "DESCRIPTION"); FixField(field, dr); // 检查是否已正确识别类型 if (field.DataType == null) WriteLog("无法识别{0}.{1}的类型{2}!", table.Name, field.Name, field.RawType); field.Fix(); list.Add(field); } return list; }
/// <summary>读取</summary> /// <param name="table"></param> /// <param name="reader"></param> /// <returns></returns> public static IDataTable ReadXml(this IDataTable table, XmlReader reader) { // 读属性 if (reader.HasAttributes) { reader.MoveToFirstAttribute(); //do //{ // switch (reader.Name) // { // case "ID": // table.ID = reader.ReadContentAsInt(); // break; // case "Name": // table.Name = reader.ReadContentAsString(); // break; // case "Alias": // table.Alias = reader.ReadContentAsString(); // break; // case "Owner": // table.Owner = reader.ReadContentAsString(); // break; // case "DbType": // table.DbType = (DatabaseType)Enum.Parse(typeof(DatabaseType), reader.ReadContentAsString()); // break; // case "IsView": // table.IsView = Boolean.Parse(reader.ReadContentAsString()); // break; // case "Description": // table.Description = reader.ReadContentAsString(); // break; // default: // break; // } //} while (reader.MoveToNextAttribute()); ReadXml(reader, table); } reader.ReadStartElement(); // 读字段 reader.MoveToElement(); while (reader.NodeType != XmlNodeType.EndElement) { switch (reader.Name) { case "Columns": reader.ReadStartElement(); while (reader.IsStartElement()) { var dc = table.CreateColumn(); (dc as IXmlSerializable).ReadXml(reader); table.Columns.Add(dc); } reader.ReadEndElement(); break; case "Indexes": reader.ReadStartElement(); while (reader.IsStartElement()) { var di = table.CreateIndex(); (di as IXmlSerializable).ReadXml(reader); table.Indexes.Add(di); } reader.ReadEndElement(); break; case "Relations": reader.ReadStartElement(); while (reader.IsStartElement()) { var dr = table.CreateRelation(); (dr as IXmlSerializable).ReadXml(reader); if (table.GetRelation(dr) == null) { table.Relations.Add(dr); } } reader.ReadEndElement(); break; default: break; } } //reader.ReadEndElement(); if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } return(table); }