检查DataReader是否为空

4

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添加记录,DataReader是一个读取数据的对象?这是你所有的代码吗? - Marcote
由于您在 while 循环的条件中使用了 dr.Read(),因此当进入循环时,dr 不能为 null。如果它是 null,则 while 循环根本不会执行。您想通过检查 dr 是否为 null 来实现什么目的? - manas
@Markust,我不使用DataReader来保存我的记录,我只是使用DataReader来获取我的数据。上面的代码并不完整,因为其中一些不是此问题的一部分,它们都可以正常工作。 - Kevin James
@silent_warrior,现在我明白了dr.Read()不能为null,我真的想检查一下这些值。 - Kevin James
5个回答

10

你的问题在于,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? - Kevin James
如果您使用的是Microsoft.VisualStudio.Data.DataReader,则应将“dr”定义为System.Data.SqlClient.SqlDataReader类型。如果不是,请直接使用上面的代码,无需定义任何内容。 - manas
什么没有起作用?请具体说明。你想要实现什么? - manas
谢谢你帮我出主意。我只是将要显示在DataGridView中的数据传递,然后创建一个条件,如果DataGridView有值,则不保存记录。然后,如果记录没有任何记录,则保存输入的数据。 - Kevin James

0

试试这个:

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

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

0
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
}

0

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

因此,else 将永远不会执行。

你可以在 while 之前使用 else 条件吗?


0

我尝试了我的想法并得到了答案。我做的是在 datagridview 中显示获取的数据,然后创建一个条件:如果 datagridview 有记录,则不保存我的记录;如果 datagridview 没有记录,则保存记录。

谢谢大家!


如果您不需要向用户显示datagridview,则只需使用我的答案中的if条件来保存记录。添加和检查datagridview可能是不必要的开销。 - manas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接