我有以下代码,当我填写名称或任何名称时,它会搜索 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 行应该在组合框中选择的同一行中显示姓名、年龄和性别。
我已经搜索了很长时间,我的知识似乎还没有达到标准。任何帮助表示赞赏!