zoukankan      html  css  js  c++  java
  • 获取mysql数据库中的表信息

     


    #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; }
        }
    
    
    
    
    
  • 相关阅读:
    程序员职业规划
    SSH框架优缺点
    Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别
    什么是J2EE,包括哪些规范!
    JS中定义类的方法
    audio.js – 随时随地,播放 HTML5 的声音
    jquery面试题里 缓存问题如何解决?
    产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复
    JAVA排序算法
    java面试题中常见的关于String类问题总结
  • 原文地址:https://www.cnblogs.com/cyqdeshenluo/p/12712064.html
Copyright © 2011-2022 走看看