简体   繁体   English

如何使用 c# 获取有关 mysql 表的所有详细信息?

[英]how to get all details about a mysql table using c#?

I want to retrieve these info for a mysql table using c#我想使用 c# 检索 mysql 表的这些信息

1) Complete column definitions including name, size and data type, and extra info like null/not null, unsigned,auto increament, default values, if data type is enum, the accepted values 1) 完整的列定义,包括名称、大小和数据类型,以及空/非空、无符号、自动递增、默认值等额外信息,如果数据类型为枚举,则接受的值

2) All constraints - Primary/Foreign/Check/Unique 2)所有约束 - 主要/外部/检查/唯一

3) All indexes 3) 所有索引

I can get column related basic info using "describe table_name" query against the database.我可以使用“describe table_name”查询数据库来获取与列相关的基本信息。

but how to fetch all these info?但是如何获取所有这些信息?

regards, Anjan问候, 安詹

just throw queries against INFORMATION_SCHEMA...只需对 INFORMATION_SCHEMA 提出查询...

For example, to get column definitions:例如,要获取列定义:

SELECT   TABLE_NAME
       , COLUMN_NAME
       , DATA_TYPE
       , CHARACTER_MAXIMUM_LENGTH
       , CHARACTER_OCTET_LENGTH 
       , NUMERIC_PRECISION 
       , NUMERIC_SCALE AS SCALE
       , COLUMN_DEFAULT
       , IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS 

Take a look at the information schema tables to get additional info.查看 信息架构表以获取更多信息。

Hope it helps.希望能帮助到你。

You can use SqlDataReader.GetSchemaTable() method and this is answer only for your 1) option, but this can be done using same connection to your schema, if your connection string has default database (schema) option set .您可以使用SqlDataReader.GetSchemaTable()方法,这仅适用于您的 1) 选项,但如果您的连接字符串设置了默认数据库(架构)选项可以使用与架构相同的连接来完成此操作。 (You do not need to create seperate connection to INFORMATION_SCHEMA schema). (您不需要创建到 INFORMATION_SCHEMA 架构的单独连接)。

More info about this method can be found here and example how to use it here .可以在此处找到有关此方法的更多信息以及如何在此处使用它的示例。

To get everything about your schema or databases use SqlClientMetaDataCollectionNames class.要获取有关架构或数据库的所有信息,请使用SqlClientMetaDataCollectionNames类。

More info about class can be found here and example how to use it here可以在此处找到有关类的更多信息以及如何在此处使用它的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM