/// <summary> /// Задаёт первичный ключ по ID в указанной таблице /// </summary> /// <param name="dv">Таблица для задания первичного ключа</param> private void CreatePrimaryKey(DataView dv) { DataTable dt = dv.Table; DataColumn[] arrEmp = new DataColumn[dt.Columns.Count]; dt.Columns.CopyTo(arrEmp, 0); DataColumn[] keysEmp = { arrEmp.First(c => c.ColumnName == "ID") }; dt.PrimaryKey = keysEmp; }
public IList <Account> GetAllDevAccounts() { var list = new List <Account>(); var properties = typeof(Account).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); var columns = new DataColumn[properties.Length]; for (int i = 0; i < properties.Length; i++) { var property = properties[i]; DataColumn column = null; foreach (DataColumn item in this["Account"].Columns) { if (item.ColumnName.Equals(property.Name, StringComparison.OrdinalIgnoreCase)) { column = item; break; } } if (column != null) { columns[i] = column; } } foreach (DataRow row in this["Account"].Rows) { var obj = new Account((Guid)row[columns.First(a => "Id".Equals(a.ColumnName))]); for (var i = 0; i < columns.Length; i++) { var column = columns[i]; if (column == null) { continue; } var property = properties[i]; if (!property.CanWrite) { continue; } var value = row[column]; if (value == DBNull.Value) { value = null; } property.SetValue(obj, value, null); } list.Add(obj); } return(list); }