您好,我正在尝试将名称添加到组合框,但我一直只得到一个结果。
while (reader.Read())
{
locationName = reader.GetString("locationName");
factoryLocationsCombo.Items.Add(locationName);
reader.NextResult();
}
这不是正确的做法吗?
最佳答案
它们不需要 reader.NextResult();
,后者在读取批处理 Transact-SQL 语句的结果时使用。如果您有一个返回一些行的查询(这里是您指定的 5
行)并且您需要遍历这些记录,那么 while(reader.Read())
将帮助您进行此迭代,因此您可以这样尝试,方法是删除 reader.NextResult();
while (reader.Read())
{
locationName = reader.GetString("locationName");
factoryLocationsCombo.Items.Add(locationName);
}
或者,您可以使用 SQLDataAdapter 将查询结果获取到 DataTable,然后将该数据表简单地绑定(bind)到 ComboBox,无需担心迭代和 while。
关于C# 将 mySQL 结果添加到组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590087/