Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-MySQL-如果状态为';a';_C#_Mysql_.net - Fatal编程技术网

C#-MySQL-如果状态为';a';

C#-MySQL-如果状态为';a';,c#,mysql,.net,C#,Mysql,.net,如果状态为a/ 这就是我的桌子的样子 |123|status| |dsg|a | |ert|b | |abc|a | 所以,我只想得到dsg和abc,因为那里的状态是a 但是我怎样才能在C#中做到这一点呢 这是我当前的代码: public string getAll() { command6 = new MySqlCommand("SELECT 123 FROM table1 WHERE status= ?status", connection); co

如果
状态为
a
/ 这就是我的桌子的样子

|123|status|
|dsg|a     |
|ert|b     |
|abc|a     |
所以,我只想得到
dsg
abc
,因为那里的
状态是
a

但是我怎样才能在C#中做到这一点呢

这是我当前的代码:

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();
}
但这只返回
dsg
,而不是
dsg
abc
:(

  • 布拉姆

您只得到一个值,因为您使用的是
ExecuteScalar()
方法,该方法用于仅返回一个值(在您的情况下,返回的第一条记录的值)

您需要使用读取器返回多个值:

 MySqlDataReader reader = command6.ExecuteReader();
 while(reader.Read())
 {
     String value = reader.GetString(0); // do something with it, e.g. put into a list
 }

您还需要更改函数的输出类型
getAll()
,以返回多个字符串值(例如数组或列表),或将数据库中的所有值连接成一个字符串。

您使用的是ExecuteScalar,它只返回第一条记录,而忽略其他记录,如msdn所说:

执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略

您需要使用:

MySqlDataReader dr = command6.ExecuteReader();
List<string> data=new List<string>();
while (dr.Read())
{
    data.Add(dr["123"].ToString());        
}
MySqlDataReader dr=command6.ExecuteReader();
列表数据=新列表();
while(dr.Read())
{
data.Add(dr[“123”].ToString());
}

ExecuteScalar()
返回结果集中第一行的第一列。@Ullas,just.ToString()不起作用。这对我来说很有效,但你发送的代码真的很差。你错过的第二行代码是
add
带有大写字母。我说的是MySQL,所以你需要一个
MySqlDataReader
,而不是
SqlDataReader
,很高兴它对你有效,实际上我已经在so编辑器中直接编码了,intellisense让我们懒惰:)