如题。数据库里有个表里面有个birth字段是注册人生日。现在想在前台显示这个人的年龄。

解决方案 »

  1.   

    select birth from tablename
      

  2.   

    先查出时间。然后用system.datetime.now.tostring()减去查出的时间。
    估计需要格式化当前时间 得到的是年龄?
      

  3.   

     System.DateTime addTime = DateTime.Parse(这里是数据库查出的时间); 
                    System.DateTime nowTime = DateTime.Parse(System.DateTime.Now.ToString());
                    System.TimeSpan timeSpan = nowTime - addTime;
                    string year= timeSpan.Year.ToString();
            
      

  4.   

    select datediff(year, birth,getdate()) from table1
      

  5.   

    不知道你在前台事要怎么显示  
      如果是TextBox你直接用获取数据库的时间的年(通过数据ukude函数可以获取)减去当前的时间的年c#代码获取  转换为string类型赋值
      如果是数据绑定控件GridView,DataLst  
    代码页面  :
      sring GetAge()
    {
    //age自己计算获取
        return age;
    }控件中选择一列转换为模板列  在项目模板里面  绑定数据   Eval(GetAge())
      

  6.   

    四楼的这方法真是简便好用啊`学习
    select datediff(year, birth,getdate()) from table1
      

  7.   

    你建立一个DataList的ItemDataBound事件,然后在事件里写如下代码:比如你的生日(Birth)是显示在一个Label上的,那么:
    Label birth=e.Item.FindControl("birth") as Label;
    然后建立SQL:
    SELECT DATEDIFF(yy,birth,getdate()) as age
    执行SQL,得到年龄(比如String Age=上面SQl执行得出的值)
    最后再把这个年龄赋给相应的单元格上:birth.text=Age