1

我在 .Net 框架 4.0 中有一个 WPF 表单。我想要做的是用查询结果填充组合框。我可以这样做,但为了防止重复代码,我试图有一个可以为每个我想填充的组合框使用/调用的类(多个组合框将共享来自同一查询的结果)。

我已经构建了我的类,如下所示:

 internal class DatabaseHandle
    {
        private string _connectionString =
            "Data Source=FINALLYWINDOWS7\\TESTING;Initial Catalog=Testing;Integrated Security=true";

        public string PopulateTeamMembers()
        {
            string queryString = "select    setting_main"
                                 + " from     [marlin].[support_config]"
                                 + " where  config_code = 30"
                                 + "         and setting_active = 1"
                                 + " order by setting_main";

            using (var connection = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(queryString, connection);

                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        return reader[0].ToString();
                    }

                    reader.Close();
                }
                catch (Exception ex)
                {
                    return "Error, unable to load";
                }
            }
            return null;
        }
    }

然后在另一个类中,我有以下方法:

private void LoadInterface()
{
    DatabaseHandle testing = new DatabaseHandle();
    string teammembers = testing.PopulateTeamMembers();

    comboBoxResolvedBy.Items.Add(teammembers);
}

这会填充我的组合框,只是它只使用查询中的第一个值填充它。我检查了数据,这方面的一切都是正确的,但我似乎无法弄清楚如何从 LoadInterface 方法枚举查询结果。

4

1 回答 1

1

请注意,当函数命中return关键字时,它会从您的函数中退出。这就是为什么它只返回一个值。它也不叫reader.close

您应该以 a 的形式返回多个值IEnumerable<string>,而不是 a string

解决此问题的一种方法是填充列表。

    public IEnumerable<string> PopulateTeamMembers()
    {
        List<string> returnList = new List<string>();
        string queryString = "select    setting_main"
                             + " from     [marlin].[support_config]"
                             + " where  config_code = 30"
                             + "         and setting_active = 1"
                             + " order by setting_main";

        using (var connection = new SqlConnection(_connectionString))
        {
            var command = new SqlCommand(queryString, connection);

            try
            {
                connection.Open();
                using(SqlDataReader reader = command.ExecuteReader())
                {
                   while (reader.Read())
                   {
                       returnList.Add(reader[0].ToString());
                   }

                   reader.Close();
                }
            }
            catch (Exception ex)
            {
                // Just let the calling class handle the exception
                throw;
            }
        }
        return returnList;
    }

然后你可以这样称呼它:

   private void LoadInterface()
   {
       DatabaseHandle testing = new DatabaseHandle();
       IEnumerable<string> teammembers = testing.PopulateTeamMembers();
       foreach(string value in teammembers)
       {
           comboBoxResolvedBy.Items.Add(value);
       }
   }
于 2012-08-11T06:32:16.247 回答