简体   繁体   中英

Get table names from a database

I've searched through a bunch of websites and I have not come across any code or tutorial which has gone through the specifics of obtaining the table names from a single database.

Assuming I have 4 databases and I want the names of all the tables within the database called mp21 , what query can I use?

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

In MySQL this will list all databases:

show databases;

For each of these you can do:

use <database_name>;

and then

show tables;

在SQL SERVER中,您可以仅使用-

select * from sys.tables

I used this query:

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

It gives me table name as well as sysDiagram(database diagram generated for that database)

use mp21
SELECT name FROM dbo.sysobjects WHERE xtype = 'U' 

这是您询问的MySQL查询。

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='mp21'

if you are asking about .net code then You need SMO :

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;



public static List<Table> GetTables(string connection, string databaseName)
        {
            if (String.IsNullOrEmpty(connection))
                throw new ArgumentException("connection is null or empty.", "connection");

            Server srv = getServer(connection);
            return srv.Databases[databaseName].Tables.Cast<Table>().ToList();
        }

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