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;
In SQL SERVER, you can just use -
select * from sys.tables
use mp21
SELECT name FROM dbo.sysobjects WHERE xtype = 'U'
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)
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();
}
Here is the MySQL query you asked.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='mp21'
精彩评论