获取文本框以显示同一行MySQL的记录

时间:2013-06-25 21:09:33

标签: c# mysql database textbox

我有以下代码,当我填写一个名称或任何名称时,它将搜索MySQL数据库并向我显示您输入的所有名称。

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

现在,这段代码很好,它用于我的comboBox。我需要的是当你按下按钮(让我们说... 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行应该在comboBox中选择的同一行中显示名称,年龄和性别。

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

1 个答案:

答案 0 :(得分: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..
     }
}

这样你就不需要查询每一点点了。我也不是百分百肯定这会完全奏效,我想你不得不有点烦恼。

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