/// <summary> /// 将一个<see cref="DataRow"/>复制到一个<see cref="DataTable"/>中 /// </summary> /// <param name="dataRow"></param> /// <param name="dataTable"><see cref="DataTable"/></param> public static void Copy(this DataRow dataRow, DataTable dataTable) { if (dataRow == null) { return; } if (dataTable == null) { dataTable = new DataTable(); } RowUtil.AddColumn(dataTable, dataRow.Table.Columns.Cast <DataColumn>().ToArray()); DataRow templateRow = dataTable.NewRow(); templateRow.ItemArray = dataRow.ItemArray; dataTable.Rows.Add(templateRow); }
/// <summary> /// <inheritdoc cref="RowUtil.AddColumn(DataTable, string[])"/> /// </summary> /// <param name="dataTable"></param> /// <param name="entityType">要添加到<see cref="DataTable"/>的对象的类型</param> public static void AddColumn(this DataTable dataTable, Type entityType) { if (dataTable == null) { return; } foreach (PropertyInfo property in entityType.GetProperties()) { RowUtil.AddColumn(dataTable, property.Name); } foreach (FieldInfo field in entityType.GetFields()) { string columnName = field.Name; if (dataTable.Columns.Contains(columnName)) { int repeatCount = dataTable.Columns.Cast <DataColumn>().Count(p => p.ColumnName.ToLower().Equals(columnName)); columnName = string.Format("{0}_{1}", field.Name, repeatCount); RowUtil.AddColumn(dataTable, columnName); } } }
/// <summary> /// <inheritdoc cref="RowUtil.AddColumn(DataTable, string[])"/> /// </summary> /// <param name="dataTable"></param> /// <param name="columns">要添加的列</param> public static void AddColumn(this DataTable dataTable, params string[] columns) { RowUtil.AddColumn(dataTable, columns); }