using System; using System.Collections.Generic; using System.Data; using System.Text; namespace EPS.Common.Data { /// /// Utility methods for working with DataTables, DataRows, et al. /// public static class DbUtility { /// /// Builds an array of values for the specified columnName from /// the rows collection. /// /// The type of data that exists in the column. /// The DataRowCollection to iterate through. /// The name of the column in the datatable, from which /// the array values are pulled. /// An array of values of the specified type created from the values /// of the specified columnName for all of the rows. public static T[] GetValues(DataRowCollection rows, string columnName) { int start = 0; int length = rows != null ? rows.Count : 0; return GetValues(rows, columnName, start, length); } /// /// Slices a range of values. /// /// The type of values that are being retrieved. /// The rows to parse. /// The name of the column containing the value to parse. /// The position of the first elemtn in rows to retrieve. /// The number of values to retrieve. /// A strongly typed array of values from a section of rows. public static T[] GetValues(DataRowCollection rows, string columnName, int start, int length) { DataRow[] rowArray = new DataRow[length]; for (int i = 0; i < length; i++) { rowArray[i] = rows[start + i]; } return GetValues(rowArray, columnName); } /// /// Builds a strongly typed array of values of a single column within an /// arrya of DataRows. /// /// The type of value in the column to retrieve. /// The array of DataRows to parse. /// The name of the column from which data values will be retrieved. /// An array of values representing all of the values /// from extracting the columnName column values from the rows array. public static T[] GetValues(DataRow[] rows, string columnName) { if (rows == null) return null; Array arr = Array.CreateInstance(typeof(T), rows.Length); for (int i = 0; i < rows.Length; i++) { object value = null; if (rows[i][columnName] != null) value = (T)rows[i][columnName]; arr.SetValue(value, i); } return (T[])arr; } /// /// Builds a strongly typed array of all of the values from the first column /// or all of the rows of the DataTable. /// /// The type of data for the resultset. /// The DataTable containing all of the values. /// A strongly typed array of values from the first column of every DataRow from /// all rows in the DataTable. public static T[] ToArray(DataTable table) { DataRow[] rows = table.Select(); Array arr = Array.CreateInstance(typeof(T), rows.Length); for (int i = 0; i < rows.Length; i++) { object value = null; if (rows[i][0] != null) value = (T)rows[i][0]; arr.SetValue(value, i); } return (T[])arr; } /// /// Builds an array of values from the column values for all rows /// in the columnName column in the DataTable. /// /// The type of data in the specified column. /// The datatable with all of the values to parse. /// The name of the column of the DataTable from which all /// values are obtained. /// An array of values from the specified column within the DataTable. public static T[] ToArray(DataTable table, string columnName) { T[] result = null; if (table == null || String.IsNullOrEmpty(columnName)) return result; result = GetValues(table.Rows, columnName, 0, table.Rows.Count); return result; } /// /// Converts a DataRowCollection to an array of DataRows. /// /// The DataRowCollection that will be converted to an /// array of DataRows. /// An array of DataRows representing each row of the DataRowCollection. public static DataRow[] ToArray(DataRowCollection rows) { DataRow[] result = null; if (rows == null) return result; result = new DataRow[rows.Count]; rows.CopyTo(result, 0); return result; } } }