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

标签 c# mysql

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

        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();

任何帮助将不胜感激

最佳答案

     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();

关于C# MySql 在 C# 中存储多个数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6167400/

相关文章:

mysql - 如何达到 "select until"和

c# - StringFormat 在设计器中显示错误,但在编译后不显示

c# - 为什么 Sql Server 数据库很忙?

mysql - 计算MYSQL日期时间

java - 结果集到二维数组

php - 找出 PHP 和 MySQL 中销售额最高和最低的一天

c# - 取字符串的最后n行c#

c# - 在 app.config 文件中写入值

c# - 可扩展的 ASP.NET MVC Web 应用程序

mysql - 获取相关表的 TIMESTAMPDIFF 的 AVG?