C#MySql在C#中存储多个数据库行

时间:2011-05-29 11:08:07

标签: c# mysql

我在这方面有点挣扎。我想从数据库中获取id列表,其中某个值等于行中的某个值。此调用可能会返回多个ID。我想将值存储在c#代码中列表或arraylist中返回的ids中,但我发现这很麻烦。我有代码到这里:

        string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
        MySqlConnection connection = new MySqlConnection(strConnection);
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader reader;
        command.CommandText = "SELECT idprojects FROM `test`.`projects` WHERE application_layers = " + applicationTiers + "";
        connection.Open();

        reader = command.ExecuteReader();

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

     string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
     MySqlConnection connection = new MySqlConnection(strConnection);

     List<string> array = new List<string>();

        using (MySqlCommand cmd = new MySqlCommand("SELECT idprojects FROM `test`.`projects` WHERE application_layers = " + applicationTiers, connection))
        {
            try
            {
                using (MySqlDataReader Reader = cmd.ExecuteReader())
                {
                    while (Reader.Read())
                    {
                        array.Add(Reader["idprojects"].ToString());
                    }
                }
            }

            catch (Exception ex)
            {
                throw;
            }
        }

        string[] ret= array.ToArray();