如果我调用SqlReader.Read,应该调用SqlDataReader.HasRows
试着看看在while (dr.read())
函数之前添加if (dr.HasRows)
是否有益。 我的意思是,从技术上讲,如果它没有行,它就不会读取,所以如果你先检查它会有关系吗?
using (SqlDataReader dr = cmd.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { ....do stuff here } } }
或者如果你只是确保它具有提供的价值,这基本上会做同样的事情……
using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { ....do stuff here } }
否(dr.HasRows)
如果DataReader包含任何行,则不必检查(dr.HasRows)
。
如果没有更多的行要读取, Read()
将返回False ,但Reader.HasRows
比Read()
更能说明它的作用,因此使用Reader.HasRows
是一个好习惯 ,因为你可能会意外地做某事除了Read()
可能会出现exception。
小心。 HasRows()为我的CTE查询返回false,即使有行(实际上是437行)。
检查DataReader是否有行(dr.HasRows)并不是必需的。 如果有更多数据要读取,Read()方法将返回true,如果没有更多数据,则返回false,从而打破while循环。
我认为这主要是针对存储过程,这些存储过程可能有也可能没有数据(一个或多个结果集),如果您还执行除while循环之外的其他操作(即初始化页眉/页脚等),则首先检查它“更容易”。什么时候有数据)。
尝试
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; } }
你的意见需要
上述就是C#学习教程:如果我调用SqlReader.Read,应该调用SqlDataReader.HasRows分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1038679.html