C# 如何检查 SQLDataReader 是否没有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12609959/
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-09 23:53:38  来源:igfitidea点击:

How to check if SQLDataReader has no rows

c#sqldatareader

提问by Tim

I am trying to figure out how to check if my SqlDataReaderis null or has no rows (meaning the reservation does not exist) and then display a messagebox. For some reason when I debug once it hits the While dr.Read())code it steps out if it does not have a return result.

我想弄清楚如何检查 mySqlDataReader是否为空或没有行(意味着预订不存在),然后显示一个消息框。出于某种原因,当我调试时,一旦遇到While dr.Read())代码,如果没有返回结果,它就会退出。

I've tried putting this code in a few different locations but none seem to fire off the messagebox if no records are returned

我试过将此代码放在几个不同的位置,但如果没有返回记录,似乎没有一个会触发消息框

if (dr.GetValue(0) == DBNull.Value || !dr.HasRows)
{
    MessageBox.Show("Reservation Number Does Not Exist","Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
    (read records)
}   

My code...

我的代码...

try
{
   using (SqlConnection con = new SqlConnection(connectionString))
   {
      using (SqlCommand cmd = con.CreateCommand())
      {
         con.Open();
         cmd.CommandText = "usp_StoredProcedureName";
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@regnum", regnumber);

         using (SqlDataReader dr = cmd.ExecuteReader())
         {
             //Loop through all the rows, retrieving the columns you need.
             while (dr.Read())
             {
                 lblConf.Text = dr.GetValue(0).ToString();
                 lblName.Text = dr.GetValue(1).ToString() + "," + dr.GetValue(2);
                 lblCompany.Text = dr.GetValue(3).ToString();
                 lblStatus.Text = dr.GetValue(4).ToString();
             }
         }
      }
   }
}
catch (Exception ex)
{
    MessageBox.Show("Can not open connection! ");
}

采纳答案by Tim Schmelter

if(dr.HasRows)
{
    // ....
}
else
{
    MessageBox.Show("Reservation Number Does Not Exist","Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}

SqlDataReader.HasRows Property

SqlDataReader.HasRows 属性

回答by David B

The HasRowspropertymay help you.

HasRows物业可能会帮助您。

Property Value

Type: System.Boolean true if the SqlDataReader contains one or more rows; otherwise false.

适当的价值

类型:System.Boolean 如果 SqlDataReader 包含一行或多行,则为 true;否则为假。

回答by Moslem7026

Add this to your code to check:

将此添加到您的代码中以进行检查:

sqlCommand cmd = new sqlCommand();
SqlDataReader dr = cmd.ExecuteReader();

if(dr.HasRows)
{
    while(dr.Read())
    {
        //code
    }
}

回答by Bob Kaufman

For some reason when I debug once it hits the while dr.Read()Code it steps out if it does not have a return result

出于某种原因,当我调试时遇到while dr.Read()代码,如果它没有返回结果,它就会退出

I think what you're seeing here is that SQLDataReader.Read()returns falseif there is not a next, or in this case a first record to read.

我想你现在看到的是SQLDataReader.Read()回报false,如果没有下一个,或在这种情况下,第一个记录读取。

As others have responded, use the HasRowsproperty to determine if you have any rows in the result set. Depending on what you need to accomplish, you may want to take advantage of the fact that Read()indeed returns falsethe first time its called for an empty result set.

正如其他人所回应的那样,使用该HasRows属性来确定结果集中是否有任何行。根据您需要完成的工作,您可能希望利用第一次调用空结果集时Read()确实返回false的事实。