/// <summary> /// Show the columns and column metadata from a specific table. /// </summary> /// <param name="tableName">The table to view.</param> /// <returns>A list of column objects.</returns> public List <Column> DescribeTable(string tableName) { if (String.IsNullOrEmpty(tableName)) { throw new ArgumentNullException(nameof(tableName)); } List <Column> columns = new List <Column>(); DataTable result = Query(MysqlHelper.LoadTableColumnsQuery(_Settings.DatabaseName, tableName)); if (result != null && result.Rows.Count > 0) { foreach (DataRow currColumn in result.Rows) { #region Process-Each-Column /* * public bool PrimaryKey; * public string Name; * public string DataType; * public int? MaxLength; * public bool Nullable; */ Column tempColumn = new Column(); tempColumn.Name = currColumn["COLUMN_NAME"].ToString(); tempColumn.MaxLength = null; if (currColumn.Table.Columns.Contains("CHARACTER_MAXIMUM_LENGTH")) { int maxLength = 0; if (Int32.TryParse(currColumn["CHARACTER_MAXIMUM_LENGTH"].ToString(), out maxLength)) { tempColumn.MaxLength = maxLength; } } tempColumn.Type = Helper.DataTypeFromString(currColumn["DATA_TYPE"].ToString()); if (currColumn.Table.Columns.Contains("IS_NULLABLE")) { if (String.Compare(currColumn["IS_NULLABLE"].ToString(), "YES") == 0) { tempColumn.Nullable = true; } else { tempColumn.Nullable = false; } } else if (currColumn.Table.Columns.Contains("IS_NOT_NULLABLE")) { tempColumn.Nullable = !(Convert.ToBoolean(currColumn["IS_NOT_NULLABLE"])); } if (currColumn["COLUMN_KEY"] != null && currColumn["COLUMN_KEY"] != DBNull.Value && !String.IsNullOrEmpty(currColumn["COLUMN_KEY"].ToString())) { if (currColumn["COLUMN_KEY"].ToString().ToLower().Equals("pri")) { tempColumn.PrimaryKey = true; } } if (!columns.Exists(c => c.Name.Equals(tempColumn.Name))) { columns.Add(tempColumn); } #endregion } } return(columns); }