예제 #1
0
        /// <summary>
        /// 得到指定db下所有table的信息
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <returns></returns>
        public static List <MysqlEntity> GetMysqlEntities(string dbName)
        {
            var    entityList = new List <MysqlEntity>();
            string dbConn     = Configuration.MysqlConnection;
            var    conn       = new MySqlConnection(dbConn);

            try
            {
                conn.Open();
                var cmd          = string.Format(@"SELECT `information_schema`.`COLUMNS`.`TABLE_SCHEMA`
                                                    ,`information_schema`.`COLUMNS`.`TABLE_NAME`
                                                    ,`information_schema`.`COLUMNS`.`COLUMN_NAME`
                                                    ,`information_schema`.`COLUMNS`.`DATA_TYPE`
                                                    ,`information_schema`.`COLUMNS`.`COLUMN_COMMENT`
                                                FROM `information_schema`.`COLUMNS`
                                                WHERE `information_schema`.`COLUMNS`.`TABLE_SCHEMA` = '{0}' ", dbName);
                var mySqlCommand = new MySqlCommand(cmd, conn);
                using (var reader = mySqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var db      = reader["TABLE_SCHEMA"].ToString();
                        var table   = reader["TABLE_NAME"].ToString();
                        var column  = reader["COLUMN_NAME"].ToString();
                        var type    = reader["DATA_TYPE"].ToString();
                        var comment = reader["COLUMN_COMMENT"].ToString();

                        var entity = entityList.FirstOrDefault(x => x.EntityName == table);
                        if (entity == null)
                        {
                            entity = new MysqlEntity(table);
                            entity.Fields.Add(new Field
                            {
                                Name    = column,
                                Type    = GetCLRType(type),
                                Comment = comment
                            });

                            entityList.Add(entity);
                        }
                        else
                        {
                            entity.Fields.Add(new Field
                            {
                                Name    = column,
                                Type    = GetCLRType(type),
                                Comment = comment
                            });
                        }
                    }
                }
            }
            finally
            {
                conn.Close();
            }
            return(entityList);
        }
        public static List <MetaTableInfo> GetMetaTableInfoListForMysql(string tableName)
        {
            var mysqlEntity       = MysqlEntity.GetMysqlEntityByTableName(tableName);
            var metaTableInfoList = new List <MetaTableInfo>();

            foreach (var item in mysqlEntity.Fields)
            {
                var metaTableInfo = new MetaTableInfo
                {
                    Name         = item.Name,
                    Annotation   = item.Comment,
                    PropertyType = item.Type
                };
                metaTableInfoList.Add(metaTableInfo);
            }

            return(metaTableInfoList);
        }