但是我如何在 C# 中做到这一点?这是我当前的代码:public string getAll() {command6 = new MySqlCommand("SELECT 123 FROM table1 WHERE status=?status", connection);command6.Prepare();command6.Parameters.AddWithValue("?status", "a");command6.ExecuteNonQuery();返回 command6.ExecuteScalar().ToString();}但这只会返回 dsg,而不是 dsg 和 abc :(布拉姆 解决方案 您正在使用 ExecuteScalar,它只返回第一条记录并忽略其他记录,如 msdn 所说:ExecuteScalar 执行查询,返回查询返回的结果集中第一行的第一列.其他列或行将被忽略.您需要使用:MySqlDataReader dr = command6.ExecuteReader();列表数据=新列表();而 (dr.Read()){data.Add(dr["123"].ToString());}I'm trying to get a value from 123 if the status is a/This is what my table looks like |123|status||dsg|a ||ert|b ||abc|a |So, I only want to get dsg and abc, because there status is a.But how can I do this in C#?This is my current code:public string getAll() { command6 = new MySqlCommand("SELECT 123 FROM table1 WHERE status= ?status", connection); command6.Prepare(); command6.Parameters.AddWithValue("?status", "a"); command6.ExecuteNonQuery(); return command6.ExecuteScalar().ToString();}But this only returns dsg, not dsg and abc :(Bram 解决方案 You are using ExecuteScalar which return only first record and ignore others as msdn say: ExecuteScalar Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.You need to use:MySqlDataReader dr = command6.ExecuteReader();List<string> data=new List<string>();while (dr.Read()){ data.Add(dr["123"].ToString()); } 这篇关于C# - MySQL - 如果状态为“a",则从列中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 19:28