c# MySqlConnection 获取组和表的名称
时间: 2024-04-08 19:30:42 浏览: 27
你可以使用以下代码来获取MySQL数据库中的所有组和表的名称:
```csharp
using MySql.Data.MySqlClient;
string connectionString = "server=localhost;user=root;database=mydatabase;password=mypassword";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// 获取所有组的名称
MySqlCommand getGroupsCommand = new MySqlCommand("SELECT DISTINCT TABLE_SCHEMA FROM information_schema.TABLES;", connection);
using (MySqlDataReader reader = getGroupsCommand.ExecuteReader())
{
while (reader.Read())
{
string groupName = reader.GetString(0);
Console.WriteLine("Group Name: " + groupName);
}
}
// 获取所有表的名称
MySqlCommand getTablesCommand = new MySqlCommand("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'your_database_name';", connection);
using (MySqlDataReader reader = getTablesCommand.ExecuteReader())
{
while (reader.Read())
{
string tableName = reader.GetString(0);
Console.WriteLine("Table Name: " + tableName);
}
}
connection.Close();
}
```
请确保在连接字符串中替换`localhost`、`root`、`mydatabase`和`mypassword`为你自己的MySQL服务器、用户名、数据库和密码。