【问题标题】:Checking whether DataReader is empty检查DataReader是否为空
【发布时间】:2012-10-22 03:13:00
【问题描述】:

DataReader 为空时,我的代码不会运行。下面是我的代码。

我的工作是关于日期安排。我的问题是关于假期限制。当用户输入日期(开始日期和结束日期)时,程序将检查输入的日期之间是否有假期。如果DataReader 没有任何数据,则应保存输入的日期,或者如果DataReader 有数据,则不保存输入的日期,程序会给出错误消息。

try
{
    econ = new SqlConnection();
    econ.ConnectionString = emp_con;
    econ.Open();
    ecmd = new SqlCommand("SELECT CD_Date FROM CONS_DATES where CD_Date between '" + Convert.ToDateTime(dtpStart.Text) + "' and '" + Convert.ToDateTime(dtpEnd.Text) + "'", econ);
    ecmd.CommandType = CommandType.Text;
    ecmd.Connection = econ;
    dr = ecmd.ExecuteReader();
    while (dr.Read())
    {
        DateTime cdname = (DateTime)dr["CD_Date"];

        //This code is working
        if (Convert.ToDateTime(cdname) >= Convert.ToDateTime(dtpStart.Text) || Convert.ToDateTime(cdname) <= Convert.ToDateTime(dtpEnd.Text))
        {
            MessageBox.Show("Holiday Constraint. Creating Record Denied.");
        } //if

        //This code is not working. When the program fetch with no record, it should be continue to add the record but it's not working
        else
        if (dr == null || !dr.HasRows)
        {
            //In this area is my code for inserting the entered data.
            MessageBox.Show("Add na|!!!. Creating Record Denied.");
        }//if else
    }//while
}//try
catch (Exception x)
{
    MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

【问题讨论】:

  • 您想如何使用读取数据的对象 DataReader 添加记录?这就是你所有的代码吗?
  • 因为你的while循环条件是dr.Read(),所以dr在循环内不能是null。如果是null,则while 循环根本不会执行。你想通过检查drnull 来达到什么目的?
  • @Markust,我没有使用 DataReader 来保存我的记录,我只是使用 DataReader 来获取我的数据。我上面的代码不完整,因为其中一些不是这个问题的一部分,它们都在工作。
  • @silent_warrior,现在我明白 dr.Read() 不能为空,我真的很想检查这些值。

标签: c# visual-studio-2010 datareader


【解决方案1】:

您的问题是,while 循环仅在 dr 有一个或多个记录时运行。但是,如果drnull,那么while 循环将永远不会运行。

更好的解决方案是拥有System.Data.SqlClient.SqlDataReader

然后检查,

if (!dr.HasRows)
{
    // Your code to save the records, if no holidays found
}
else
{
    // Your code to show the error message, if there is one or more holidays
}

【讨论】:

  • 我应该在哪里添加 System.Data.SqlClient.SqlDataReader?谢谢
  • dr 应该是System.Data.SqlClient.SqlDataReader 的类型,我假设您使用的是Microsoft.VisualStudio.Data.DataReader。如果不需要定义任何东西,只需使用上面的代码即可。
  • 什么不起作用?请具体。你想达到什么目标?
  • 感谢您帮助我提出一个想法。我只是传递要在 datagridview 中显示的数据,然后创建一个条件,如果 datagridview 有值,则不会保存记录。那么如果该记录没有任何记录,则保存输入的数据。
【解决方案2】:

我尝试了我的想法,它回答了我的问题。我所做的是在 datagridview 中显示获取的数据,然后我创建了一个条件,如果 datagridview 有记录,那么我的记录将不会被保存,如果 datagridview 没有任何记录,那么记录将被保存。

谢谢大家!

【讨论】:

  • 如果您不需要向用户显示数据网格视图,则只需使用我的答案中的 if 条件来保存记录。添加和检查 datagridview 可能是不必要的开销
  • @silent_warrior,谢谢。我记得。
【解决方案3】:

试试这个:

while (sqlreader.Read())
{
    tbSubscriptionInfo.Text = sqlreader["nr_ore"].ToString();
}

if (!sqlreader.HasRows)
{
    tbSubscriptionInfo.Text = "";
}

【讨论】:

    【解决方案4】:
    if (dr == null || !dr.HasRows)
    {
        // Your code to show the error message, if there is one or more holidays       
    }
    else
    {
        // Your code to save the records, if no holidays found
    }
    

    【讨论】:

      【解决方案5】:

      while(dr.Read()) 表示 dr 至少有 1 行。

      所以 else 永远不会执行

      你能在while之前做其他条件吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-23
        • 2013-07-18
        • 2013-07-12
        • 2013-07-27
        • 2015-08-09
        相关资源
        最近更新 更多