Getting a list of databases on a MySQL server

Getting a list of databases on a MySQL server

Postby ashlar64 » Tue 01 Jul 2008 12:46

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
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56

Postby pleb » Wed 02 Jul 2008 02:52

Try

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

Example

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = "tableToLookFor"
pleb
 
Posts: 37
Joined: Mon 23 Jun 2008 06:21
Location: Melbourne, Australia

Postby anton.connect » Wed 02 Jul 2008 10:42

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);
}
anton.connect
 
Posts: 43
Joined: Thu 19 Jun 2008 14:30

Postby ashlar64 » Wed 02 Jul 2008 12:27

That works great! Thanks Anton!!!

:D
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56


Return to dotConnect for MySQL