PDA

View Full Version : c# how to remove MySQL's default databases


telmessos
08-29-2010, 06:59 PM
Hi all,

I am using the following code to load the databases on MySQL to a ComboBox. But the combobox shows also the default databases like information_schema, mysql and test databases. I would like to exclude these databases from the list.

Could you please tell me how I can exclude certain databases?

Thanks
telmessos


string sql = "Show databases";
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand();
command.CommandText = sql;
command.Connection = connection;
adapter.SelectCommand = command;
DataTable dtListe = new DataTable();
adapter.Fill(dtListe);
connection.Close();
comboBox1.DataSource = dtListe;
comboBox1.DisplayMember = "DATABASE";
comboBox1.ValueMember = "DATABASE";

ess
09-07-2010, 09:17 PM
You should create a list of all the items that you don't wish to include in the drop down and then iterate over the data table rows checking if the object name exists in the list.

here is an example


// declare a list to contain objects that you don't wish to include
List<string> notToIncluded = new List<string>();
// start adding the objects to the list
notToIncluded.Add("MySql");
notToIncluded.Add("Test");
// add more to the list if you wish

// iterate over the data table rows
foreach (DataRow row in dtListe.Rows)
{
// check if the object's name exists in the list
// and remove it from the table if it does
if (notToIncluded.Contains( row[0].ToString()))
{
dtListe.Rows[0].Delete();
dtListe.AcceptChanges();
} //-- ends if
} //-- ends foreach


Update the data table before calling the following statement from your code


comboBox1.DataSource = dtListe;
// why aren't binding the combobox to the data source


Cheers
~E