C# 如果我调用 SqlReader.Read,我应该调用 SqlDataReader.HasRows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14196644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:01:54  来源:igfitidea点击:

Should I call SqlDataReader.HasRows if I am calling SqlReader.Read

c#sql.netmethodssqldatareader

提问by Joshua Volearix

Trying to see if it is beneficial to add an if (dr.HasRows)before the while (dr.read())function. I mean, technically if it doesn't have rows it isn't going to read, so would it matter if you checked this first?

尝试看看if (dr.HasRows)while (dr.read())函数之前添加一个是否有益。我的意思是,从技术上讲,如果它没有行,它就不会读取,所以如果你先检查这个会有关系吗?

using (SqlDataReader dr = cmd.ExecuteReader())
{
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            ....do stuff here
        }
    }
}

or is this going to essentially do the exact same thing if you're just making sure it has values to provide...

或者,如果您只是确保它具有要提供的价值,那么这基本上会做完全相同的事情吗?

using (SqlDataReader dr = cmd.ExecuteReader())
{
    while (dr.Read())
    {
        ....do stuff here
    }
}    

采纳答案by Vishal Suthar

No..It is not mandatory to check (dr.HasRows)if the DataReader contains any row or not.

(dr.HasRows)不。检查DataReader 是否包含任何行不是强制性的。

Read()will return Falseif there are no more rows to fetch, but Reader.HasRowsis much more telling as to what it does than Read()so it would be a good practiceto use Reader.HasRowsbecause you may accidentally do something other than Read()which may fall into exception.

Read()如果没有更多的行要提取,将返回False,但Reader.HasRows它更能说明它的作用,Read()因此使用它会是一个很好的做法Reader.HasRows因为您可能会意外地做一些Read()可能陷入异常的事情。

回答by TomTom

I think this is mostly for stored procedures which may or may not have data (one or more result sets) and it is "easier" to check first in case you also do other stuff than the while loop (i.e. initialize header/footer etc. when there is data).

我认为这主要是针对可能有也可能没有数据(一个或多个结果集)的存储过程,并且如果您还执行 while 循环以外的其他操作(即初始化页眉/页脚等),则首先检查“更容易”。有数据时)。

回答by Abbas

It's not mandatory to check if the DataReader has rows (dr.HasRows). The Read() method will return true if there is more data to read and false if there's no more data, thus breaking the while-loop.

检查 DataReader 是否有行 (dr.HasRows) 不是强制性的。如果有更多数据要读取,Read() 方法将返回 true,如果没有更多数据,则返回 false,从而破坏 while 循环。

回答by adnan

try

尝试

            string myconnection = "datasource= localhost;port=3306;username=root;password=root;";
            MySqlConnection myconn = new MySqlConnection(myconnection);

            //MySqlDataAdapter mydata = new MySqlDataAdapter();
            MySqlDataReader myreader;

            MySqlCommand SelectCommand = new MySqlCommand("select *from student_info.student_info where username= '" + textBox1.Text +" 'and password=' " + textBox2.Text +"';",myconn );


            myconn.Open();

            myreader = SelectCommand.ExecuteReader();
            int count = 0;
            if (myreader.HasRows) //returing false but i have 4 row
            {
                while (myreader.Read()) //returing false 
                {
                    MessageBox.Show("in button3");
                    count = count + 1;
                }
            }

your opinion required

需要你的意见

回答by Chalky

Be careful. HasRows() returns false for my CTE query, even though there are rows (437 rows actually).

当心。HasRows() 为我的 CTE 查询返回 false,即使有行(实际上是 437 行)。