如何排除将特定数据库添加到我的阵列?

How do I exclude specific databases from being added to my array?

我想排除特定数据库的显示,但我排除的项目仍然显示在 运行 程序中。没有编译错误。

    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());
    }
}

你的逻辑不正确。我假设这些都是 table 您不想 添加到您的数组中的名称。

  • 假设您要排除 "information_schema"。该名称不等于 "sakila",因此该名称将添加到您的数组中。
  • 此外,由于您只是在此之前定义了databases数组,并且其中的所有元素都是空的(或null?),none将满足这些条件。

将所有 || 更改为 &&,并将它们与 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++;
    }
}

我认为一些 LINQ 和使用 List<string> 会使它更易于阅读。它还将消除对获取用于在数组中分配 space 的计数的第一个查询的需要。

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);
}