I want to exclude specific database from showing up, but the items that I excluded still show up upon running the program. There is no compilation error.
private ComboBox cb;
private Label label;
private object[] databases;
public MForm() {
string connectionString =
"Server=localhost;" +
"Database=information_schema;" +
"User ID=root;" +
"Password=root;" +
"Pooling=false";
IDbConnection dbcon;
dbcon = new MySqlConnection(connectionString);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
string sql = "SELECT COUNT(*) as count FROM information_schema.SCHEMATA"; //count the databases(string) and names it count
dbcmd.CommandText = sql; //sends the string to sql
IDataReader reader = dbcmd.ExecuteReader(); //assign the function to reader
reader.Read(); //uses its getter(Read)
int count = Convert.ToInt32(reader["count"]); //converts the "count"(column) to integer
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
databases = new object[count];
dbcmd = dbcon.CreateCommand();
sql = "show databases";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
int i = 0;
while(reader.Read()) {
if((string) databases[i] != "information_schema" &&(string)databases[i] != "sakila" && (string)databases[i] != "enrollmentsystem" && (string)databases[i] != "mysql" &&
(string)databases[i] != "world" && (string)databases[i] != "performance_schema"){
databases[i] = (string) reader["Database"];
i++;
}
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
Text = "School Year";
Size = new Size(340, 240);
cb = new ComboBox();
cb.Parent = this;
cb.Location = new Point(50, 30);
cb.Items.AddRange(databases);
cb.SelectionChangeCommitted += new EventHandler(OnChanged);
label = new Label();
label.Location = new Point(80, 170);
label.Parent = this;
label.Text = "...";
CenterToScreen();
}
void OnChanged(object sender, EventArgs e) {
ComboBox combo = (ComboBox) sender;
label.Text = combo.Text;
}
}
class MApplication {
public static void Main() {
Application.Run(new MForm());
}
}
Your logic is incorrect. I'm assuming these are all table names that you do not want to add to your array.
databases
array before this, and all the elements in it are empty (or null?), none of these conditions will be met.Change all the ||
to &&
, and compare them to the data returned in your reader:
while (reader.Read())
{
var database = reader["Database"].ToString();
if ((database != "information_schema" && database != "sakila" &&
database != "enrollmentsystem" && database != "mysql" &&
database != "world" && database != "performance_schema")
{
databases[i] = database;
i++;
}
}
I think some LINQ, and using a List<string>
, would make this easier to read. It would also eliminate the need for the first query that gets the count used to allocate space in your array.
var databases = new List<string>();
var excludedDatabases =
new List<string> { "information_schema", "sakila", "enrollmentsystem",
"mysql", "world", "performance_schema" };
while (reader.Read())
{
var database = reader["Database"].ToString();
if (!excludedDatabases.Contains(database))
databases.Add(database);
}