![](https://img2020.cnblogs.com/blog/1276461/202004/1276461-20200416125433503-1659296504.png)
#region 获取数据库表信息列表 /// <summary> /// 获取数据库表信息列表 /// </summary> /// <param name="input"></param> /// <returns></returns> public Task <List<DetailMessageInfoDto>> GetTableList(string input) { using (MySqlConnection conn = GetConnection()) { List<DetailMessageInfoDto> dic = new List<DetailMessageInfoDto>(); MySqlCommand cmd = conn.CreateCommand(); if (string.IsNullOrEmpty(input)) { cmd.CommandText = "select table_name from information_schema.tables where table_schema='"+ dataName + "' and table_name <>'__efmigrationshistory'"; } else { cmd.CommandText =string.Format("select table_name from information_schema.tables where table_schema='"+ dataName + "' and table_name like '%{0}%'",input); } using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { string table = reader.GetString("table_name"); DetailMessageInfoDto model = new DetailMessageInfoDto() { TableName = table }; dic.Add(model); } } conn.Close(); return Task.FromResult(dic); } } #endregion
#region 根据表名获取数据库表字段信息 /// <summary> /// 根据表名获取数据库表字段信息 /// </summary> /// <param name="table"></param> /// <returns></returns> public Task<List<MyTable>> GetFieldList(string table) { using (MySqlConnection conn = GetConnection()) { Dictionary<string, MyTable> dic = new Dictionary<string, MyTable>(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText =string.Format("select table_name,column_name,data_type,column_comment from information_schema.columns where table_schema='"+ dataName + "' and table_name='{0}'",table); using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Columns columns = new Columns(); string column = reader.GetString("column_name"); string types = reader.GetString("data_type"); string comment = reader.GetString("column_comment"); columns.ColumnName = column; columns.TypeName = types; columns.comment = comment; if (dic.ContainsKey(table)) { dic[table].ColumnList.Add(columns); } else { MyTable t = new MyTable(); t.Table = table; t.ColumnList.Add(columns); dic.Add(t.Table, t); } } } conn.Close(); return Task.FromResult(dic.Values.ToList()); } } #endregion public MySqlConnection GetConnection() { MySqlConnection conn = new MySqlConnection(ConfigurationManager.conns); conn.Open(); return conn; }
public class MyTable { public string Table { get; set; } public List<Columns> ColumnList { get; set; } = new List<Columns>(); } public class Columns { public string ColumnName { get; set; } public string TypeName { get; set; } public string comment { get; set; } }
public class DetailMessageInfoDto { /// <summary> /// 描述 /// </summary> public string TableName { get; set; } }