Hi
I want to write a query to get the names of tables of a specific database, but I don't know how can write it.
I want to execute this query for MySql.
Hi
I want to write a query to get the names of tables of a specific database, but I don't know how can write it.
I want to execute this query for MySql.
If you have the correct permissions you can use the following SQL
show tables;
That will return a single column from the database with a list of tables in it.
The field name should be Tables_in_databasename
this command will give you the list of tables.
use mine;
show tables;
Here is a better guide, that shows a C# method for getting table names from a MySQL Database.
If you need help in creating a MySQL Connection string here is an example
Download mysql connector from here: http://dev.mysql.com/downloads/connector/net/
Use this code:
using MySql.Data.MySqlClient;
string myConnectionString = "SERVER=localhost;" +
"DATABASE=mydatabase;" +
"UID=user;" +
"PASSWORD=mypassword;";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SHOW TABLES;";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + ", ";
Console.WriteLine(row);
}
connection.Close();
another way by using the ANSI INFORMATION_SCHEMA view
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'