1

我有以下代码,当我填写名称或任何名称时,它会搜索 MySQL DB 并向我显示包含您输入内容的每个名称。

MySqlConnection connection2 = new MySqlConnection("Server=" + server + ";" + "Port=" + port + ";" + "Database=" + database + ";" + "Uid=" + uid + ";" + "Password=" + password + ";");


            connection2.Open();
            string query = @"SELECT DISTINCT name2 FROM childDatabase WHERE name2 Like '%" + childSearch.Text + "%'";
                    MySqlCommand cmd = new MySqlCommand(query, connection2);

                cmd.ExecuteNonQuery();

                MySqlDataAdapter da = new MySqlDataAdapter(query, connection2);
                DataTable dt = new DataTable();
                da.Fill(dt);


                childSearchCombo.DataSource = dt;
                childSearchCombo.ValueMember = dt.Columns[0].ColumnName;
                connection2.Close();

现在,这段代码很好,它用于我的组合框。我需要的是,当您按下按钮时(比如说... btnShow),它应该在同一行中显示从数据库中检索到的其余部分。现在它目前只在 [0] 处显示索引,这对于每条记录显然不是动态的,并且是我理解的代码中的明显缺陷。

这是它的代码:

        MySqlConnection connection2 = new MySqlConnection("Server=" + server + ";" + "Port=" + port + ";" + "Database=" + database + ";" + "Uid=" + uid + ";" + "Password=" + password + ";");

        connection2.Open();
        string query = @"SELECT DISTINCT name2, age, gender FROM childDatabase";
        MySqlCommand cmd = new MySqlCommand(query, connection2);

        cmd.ExecuteNonQuery();

        MySqlDataAdapter da = new MySqlDataAdapter(query, connection2);
        DataTable dt = new DataTable();
        da.Fill(dt);


        firstNameDisp.Text = dt.Columns[0].ColumnName;
        ageDisp.Text = dt.Columns[1].ColumnName;
        genderDisp.Text = dt.Columns[2].ColumnName;

所以基本上该代码的最后 3 行应该在组合框中选择的同一行中显示姓名、年龄和性别。

我已经搜索了很长时间,我的知识似乎还没有达到标准。任何帮助表示赞赏!

4

1 回答 1

2

您应该使用using语句来正确处理您的 MySQL 对象。

这是一个帮助您入门的代码片段。

DataTable dt = new DataTable();
string _CS = "Server=" + server + ";Port=" + port + ";Database=" + database + ";Uid=" + uid + ";Password=" + password;
using (MySqlConnection connection2 = new MySqlConnection(_CS))
{
    connection2.Open();
    string query = @"SELECT DISTINCT * FROM childDatabase";
    using (MySqlCommand cmd = new MySqlCommand(query, connection2))
    {
        // cmd.ExecuteNonQuery(); There's no need to execute this. da.Fill() will 
        // execute your command.
        using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
            da.Fill(dt);
    }
    // connection2.Close(); No need to close either. The using statement does that.
}

foreach (var st in dt.AsEnumerable())
{
    if (st.Field<string>("name2").Contains(childSearch.Text))
    // or .Contains(yourComboBox.SelectedItem.ToString())
    {
        childSearchCombo.Items.Add(st.Field<string>("name2"));
        firstNameDisp.Text = st.Field<string>("your column name");
        ageDisp.Text = st.Field<string>(0); // or by index
        genderDisp.Text = st.Field<string>("column name or index");
        // Note that st.Field<T> also can be a decimal, a bool, an int etc..
     }
}

这样你就不需要查询每一点。我也不是 100% 确定这会完全奏效,我认为你必须解决一些问题。

无论如何,可能有很多更好的方法来执行此操作。但我认为这是一个很好的学习曲线。祝你好运!

于 2013-06-26T20:40:59.750 回答