/// <summary> /// 把DataTable填充到数据容器中,方便填充数据到DataWindow /// </summary> /// <param name="dataTable">数据容器</param> /// <param name="dt">DataTable</param> /// <Author>jianghaoqun 2007-06-07</Author> public static void Fill(LiteDataTable dataTable, DataTable dt) { int columnsCount = dataTable.Columns.Count; if (columnsCount == 0) { columnsCount = dt.Columns.Count; for (int i = 0; i < dt.Columns.Count; i++) { dataTable.Columns.Add(new LiteDataColumn(dt.Columns[i].ColumnName, dt.Columns[i].DataType)); } } foreach (DataRow dr in dt.Rows) { var row = dataTable.NewRow(); for (int i = 0; i < columnsCount; i++) { var value = dr[i]; if (value != DBNull.Value) { row[i] = value; } } dataTable.Rows.Add(row); } }
/// <summary> /// 根据SQL Command返回的IDataReader填充数据容器 /// <para> /// 对于DBNull的值,填充null /// </para> /// </summary> /// <param name="dataTable">数据容器</param> /// <param name="reader">SQL语句返回的</param> public static void Fill(LiteDataTable dataTable, IDataReader reader) { if (dataTable.Columns.Count == 0) { AddColumns(dataTable, reader); } int columnsCount = dataTable.Columns.Count; while (reader.Read()) { var row = dataTable.NewRow(); for (int i = 0; i < columnsCount; i++) { var value = reader[i]; if (value != DBNull.Value) { row[i] = value; } } dataTable.Rows.Add(row); } }