Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 在C中有一条记录的While循环_C#_C# 4.0 - Fatal编程技术网

C# 在C中有一条记录的While循环

C# 在C中有一条记录的While循环,c#,c#-4.0,C#,C# 4.0,如果上面的查询只返回一条记录,那么它不会经过while循环。只有当select查询中有多行时,才会执行while循环。那么,我怎样才能将这个while循环更改为在两种情况下都执行c中的一条记录或多条记录呢 OracleCommand command = connection.CreateCommand(); command.CommandText = "select * from test where id = 2"; int rowcount = 0;

如果上面的查询只返回一条记录,那么它不会经过while循环。只有当select查询中有多行时,才会执行while循环。那么,我怎样才能将这个while循环更改为在两种情况下都执行c中的一条记录或多条记录呢

OracleCommand command = connection.CreateCommand();                   
command.CommandText = "select * from test where id = 2";

int rowcount = 0;
while (reader.Read())
{
    rowcount++;
}
if (rowcount == 0)
{
     MessageBox.Show("No reords found");
       return;
}

您可以使用以下内容:

if (reader.Read())
{
    // at least one record available
    do
    {
        // work with reader
    } while (reader.Read()); // true while another records available
}
else
{
    // no records found
}

我们看不到所有的代码,所以我们也看不出它有什么问题,但通常有些时候ppl会忘记以这种方式实际调用读取器,而while循环条件永远不会发生
将表名放在[]中也更安全。

读取器变量的类型是什么?代码不完整。我们看不到reader来自何处。通常对于OracleDbReader这样的.NET阅读器,您需要调用reader=command.ExecuteReader;然后为每一行调用reader.Read。请参阅。如果reader.Read返回false,则查找代码可能没有结果。Downvoter,请阅读作者的问题。我的信息与这两种情况完全对应。你能解释一下为什么OP的代码不起作用,以及你的代码版本如何更好吗?@sstan-OP的代码起作用。我的朋友也是。但不需要计票,谢谢!reader.Read对我来说返回false。我可能会被张贴这一过早。我现在正在调试它。再次感谢您的回复