C#:我应该使用"若"而不是"而"当读者只有一个元素? [英] C#: Should I use "if" instead of "while" when a Reader has only one element?

查看:179
本文介绍了C#:我应该使用"若"而不是"而"当读者只有一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个:

  myReader = cmd.ExecuteReader(); 
如果(myReader.HasRows)
{
,而(myReader.Read())
{
信息= myReader.GetString(信息);

}
}
.....

好吧,我不知道为什么,但是当我编译它,并在另一台PC使用它,它爆炸的,因为在,而。但是,如果我做的:

  myReader = cmd.ExecuteReader(); 
如果(myReader.HasRows)
{
如果(myReader.Read())< -------------------的通知IF
{
信息= myReader.GetString(信息);

}
}
.....

...会工作。请注意,我知道我将有只有一个元素每一次,因为程序的逻辑。



这是确定这样做吗?它看起来可怕,也许不是一个好的做法()。 。I'm询问您的方位



修改
将发布更多的代码,因为可以找到我的错误的帮助:

 而(myReader.Read())
{
信息= myReader.GetString(信息 );



/ **
*写日志
* /
的Connection.close相关信息(); < ----------:•
connection.Open(); < ----------如果我不这样做我得到了一些问题与连接!!!!
串queryLog =INSERT INTO ...;
的MySqlCommand的cmdlog =新的MySqlCommand(queryLog,连接);
cmdLog.ExecuteNonQuery();
}


解决方案

我做到这一点,每当我期待只是 一行,没有错吧。



如果我期待一个值,我用的ExecuteScalar 来代替。这将简单地返回的结果集的第一行的第一列举行的返回值,usally设定只有一列,一行,所以你不容易注意到这一点。



此外,我倾向于不与如果(myReader.HasRows)如果打扰(myReader.Read ())涵盖这一点。



我不知道为什么,而使你问题...我只是提供使用我看来如果如果你不期待,而多行。


Look at this:

myReader = cmd.ExecuteReader();
if (myReader.HasRows)
{
    while (myReader.Read())
    {
        info = myReader.GetString("info");
        ...
    }
 }
.....

Well, I don´t know why, but when I compile it and use it in another PC, it explodes because of the while. But If I do:

myReader = cmd.ExecuteReader();
if (myReader.HasRows)
{
    if (myReader.Read())    <------------------- NOTICE THE IF
    {
        info = myReader.GetString("info");
        ...
    }
 }
.....

... will work. Notice that I know that I will have only one element every time, because of the logic of the program.

It is OK to do this? It looks horrible and maybe is not a good practice (?). I´m asking for your orientation.

EDIT: Will post some more code, because could help in finding my errors:

 while (myReader.Read())
 {
    info = myReader.GetString("info");
    ...


     /**
      * Write the relevant info in the LOGS
      */
      connection.Close();     <----------   :S  
      connection.Open();      <---------- if I don´t do this I got some problems with the connection!!!!
      string queryLog = "INSERT INTO ....;
      MySqlCommand cmdLog = new MySqlCommand(queryLog, connection);
      cmdLog.ExecuteNonQuery();
 }

解决方案

I do this whenever I am expecting just one row, nothing wrong with it.

If I'm expecting a single value, I use ExecuteScalar instead. This will simply return the value held in the first column of the first row of the resultset returned, usally the set only has one column and one row, so you don't tend to notice this.

Also I tend not to bother with if (myReader.HasRows) as if (myReader.Read()) covers this.

I have no idea why the while causes you issues... I'm just offering my opinion on using the if over the while if you aren't expecting multiple rows.

这篇关于C#:我应该使用&QUOT;若&QUOT;而不是&QUOT;而&QUOT;当读者只有一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆