我的代码有一点问题。我想用数据库字段填充组合框:
const string connMysql = "DataSource=localhost; Database=ph; Uid=root; Pwd=;";
myConn = new MySqlConnection(connMysql);
string cad = "SELECT answers FROM `questions` WHERE (`test_num` = '1')";
myConn.Close();
myConn.Open();
myCommand = new MySqlCommand(cad, myConn);
myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
CB1.Items.Clear();
CB1.Items.Add(myReader[0].ToString());
}
我使用它,在
combobox
中仅保留1个字段,我想填写“答案”的所有内容。 最佳答案
正如其他人所述,您只添加一个字段,即可遍历查询。
const string connMysql = "DataSource=localhost; Database=ph; Uid=root; Pwd=;";
myConn = new MySqlConnection(connMysql);
string cad = "SELECT answers FROM `questions` WHERE (`test_num` = '1')";
myConn.Close();
myConn.Open();
myCommand = new MySqlCommand(cad, myConn);
myReader = myCommand.ExecuteReader();
int i = 0;
CB1.Items.Clear(); //Clear the Combo box once before adding values
while(myReader.Read())
{
CB1.Items.Add(myReader[i].ToString());
i++;
}