简体   繁体   中英

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

I want to retrieve these info for a mysql table using c#

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

2) All constraints - Primary/Foreign/Check/Unique

3) All indexes

I can get column related basic info using "describe table_name" query against the database.

but how to fetch all these info?

regards, Anjan

just throw queries against 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 . (You do not need to create seperate connection to INFORMATION_SCHEMA 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.

More info about class can be found here and example how to use it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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