在这些日期并没有采取从其他日期采取的数据库

时间:2010-07-02 10:07:41

标签: c#

string strqry = "select dob from master_studentpersonal where stud_id= '" +
                                                           TextBox1.Text + "'";
DataTable dt = mc.selectQryDataTable2(strqry);
DataView dv = new DataView(dt);
DateTime dob = new DateTime();
if (dob != null)
{
    if (dob <= new DateTime(2004, 01, 01) &&
        dob <= new DateTime(2005, 12, 31))
    {
        TextBox6.Text = "Group A";

    }
    else if (dob <= new DateTime(2003, 12, 31) &&
             dob <= new DateTime(2002, 01, 01))
    {
        TextBox6.Text = "Group B";
    }
}

请帮助我的朋友

4 个答案:

答案 0 :(得分:1)

您尚未将SQL查询中的值分配给dob变量。 dob只能设置为空构造函数附带的值。 此外,正如Chris F所说,检查两个if语句的条件:当前如果日期小于或等于31/12/2005,则该组将始终为A.

答案 1 :(得分:0)

您的TextBox每次都会说“A组”:

DateTime dob = new DateTime();
if (dob != null)    // dob can't be null unless you haven't instantiated it, 
                    // it's a value type. Actually it's 
                    // value here is always DateTime.MinValue, 
                    // since you aren't assigning anything to it.

{
    if (dob <= new DateTime(2004, 01, 01) &&
        dob <= new DateTime(2005, 12, 31))    // this second check is redundant.
                                              // if this condition is ever evaluated,
                                              // it will always be true. I assume you have
                                              // mixed up your < and > signs here  
    {
        TextBox6.Text = "Group A";

    }
    else if (dob <= new DateTime(2003, 12, 31) &&  // as above, this statement can never
                                                   // evaluate to true. you won't even
                                                   // reach this code unless dob > 2004
             dob <= new DateTime(2002, 01, 01))
    {
        TextBox6.Text = "Group B";
    }
}

答案 2 :(得分:0)

dob永远不会为空,因为您分配了它,因为DateTime是值类型。不能解决您的问题,但它会整理您的代码;-)

更重要的是,你永远不会给它赋值,所以它会选择像DateTime.MinValue这样的任意值(猜测)。

答案 3 :(得分:0)

除了Dan Iveson的回答 - 你的输出将永远是“A组”,因为2003-12-31&lt; = 2004-01-01。

我猜你的意思是:

string strqry = "select dob from master_studentpersonal where stud_id= '" + 
                                                           TextBox1.Text + "'"; 
DataTable dt = mc.selectQryDataTable2(strqry); 
DataView dv = new DataView(dt); 
DateTime dob = // get date of birth from dt
if (dob != null) 
{ 
    if (dob <= new DateTime(2003, 12, 31) && 
             dob >= new DateTime(2002, 01, 01)) 
    { 
        TextBox6.Text = "Group B"; 
    } 
    else if (dob >= new DateTime(2004, 01, 01) && 
        dob <= new DateTime(2005, 12, 31)) 
    { 
        TextBox6.Text = "Group A";  
    } 
}