Page 1 of 1

Getting a list of databases on a MySQL server

Posted: Tue 01 Jul 2008 12:46
by ashlar64
Hello,

I am wondering how you can do this...

I need to get a list of databases after I connect to a MySQL server. To be included in this list the database needs to pass some criteria. Basically if the database has particular named tables I want to add this to the list. (My ultimate goal is to allow the user to select which database to connect to)

Thanks,

---Dave

Posted: Wed 02 Jul 2008 02:52
by pleb
Try

http://dev.mysql.com/doc/refman/5.0/en/ ... chema.html

Example

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = "tableToLookFor"

Posted: Wed 02 Jul 2008 10:42
by anton.connect
You can use the following code to get the list of databases stored on the server side and also to add a database to the list if it contains a table with the specified name:

List databasesList = new List();
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();

DataTable table1 = connection.GetSchema("Databases");
foreach (DataRow row in table1.Rows) {
string dbName = row[0].ToString();
DataTable table2 = connection.GetSchema("Tables", new string[] { dbName, "TableToLookFor" });

if (table2.Rows.Count > 0)
databasesList.Add(dbName);
}

Posted: Wed 02 Jul 2008 12:27
by ashlar64
That works great! Thanks Anton!!!

:D