我想用数据库信息填充组合框,我正在存储我的信息:
CREATE TABLE `websites`
(
`id` int(11) NOT NULL,
`name` varchar(125) NOT NULL,
`xpath` varchar(250) NOT NULL,
`time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `websites`
--
INSERT INTO `websites` (`id`, `name`, `xpath`, `time`)
VALUES
(1, 'Dailymail', '//div[@itemprop=\\\"articleBody\\\"]', '2017-04-30 23:34:49'),
(2, 'Skynews', '/html/body/div[1]/div[2]/div[1]/div[3]/div', '2017-04-30 23:35:29'),
(3, 'BBC news', '//*[@id=\\\"page\\\"]/div[2]/div[2]/div/div[1]/div[1]/div[2]', '2017-04-30 23:35:44'),
(4, 'Yahoo news', '//*[@id=\\\"Col1-0-ContentCanvas\\\"]/article', '2017-04-30 23:35:57'),
(5, 'CNBC', '//*[@id=\\\"article_body\\\"]', '2017-04-30 23:36:10'),
(6, 'CNN', '//*[@id=\\\"body-text\\\"]', '2017-04-30 23:36:27'),
(7, 'Fox news', '//*[@id=\\\"content\\\"]/div[2]/div[3]/article/div/div[3]/div[2]', '2017-04-30 23:36:40');
我想将文字显示为" name"从我的数据库中为每个单独的项目,当我尝试以下它只做一个索引,我怎么能将它们分开?
scon.Open();
scmd = new MySqlCommand();
string SQL = "SELECT * FROM websites";
scmd.Connection = scon;
scmd.CommandText = SQL;
MySqlDataReader rdr = scmd.ExecuteReader();
int count = rdr.FieldCount;
while (rdr.Read())
{
for (int i = 0; i < count; i++)
{
newssite.Text += rdr.GetValue(i);
}
}
答案 0 :(得分:2)
否则
newssite.Text += rdr.GetValue(i)
与
相同newssite.Text = newssite.Text + rdr.GetValue(i)
所以你只是一直追加到同一个元素。 在你的while循环中做这样的事情。在这里我假设,newssite是你的组合框,请在以后的问题中更具体。
while (rdr.Read())
{
newssite.Items.Add(rdr["name"].ToString());
}