我对此有点挣扎。我想从数据库中获取 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/