通过DataSet取到MS SQL里datatime类型的Date字段
如何与当前时间作比较?
System.DateTime current=DateTime.Now;
SqlDataAdapter date= new SqlDataAdapter("Select * from Shift_Cal where Day_Shift like 'A'",conn);
DataSet ds=new DataSet();
date.Fill(ds,"date");
System.DateTime gettime=ds.Tables["date"].Rows[0]["Date"];
if (gettime > current) this.Label6.Text="Y";Build报错:System.DateTime gettime=ds.Tables["date"].Rows[0]["Date"];
Cannot implicitly convert type 'object' to 'System.DateTime'

解决方案 »

  1.   

    System.convert.toDatetime(ds.Tables["date"].Rows[0]["Date"])
      

  2.   

    System.DateTime gettime=DateTime.Parse(ds.Tables["date"].Rows[0]["Date"].ToString());
      

  3.   

    反一下:current到SQL语句作查询时的变量,如何做?
    SqlDataAdapter getshift=new SqlDataAdapter("Select * from Shift_Cal where Date > currenttime",conn);
    DataSet ds=new DataSet();
    getshift.Fill(ds,"shift");
    string currentshift=ds.Tables["shift"].Rows[0]["Shift"].ToString();
    this.Label6.Text=currentshift;
    为什么报错啊?
    多谢指导!
      

  4.   

    这样试试:string current=DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
    SqlDataAdapter date= new SqlDataAdapter("Select * from Shift_Cal where Day_Shift like 'A'",conn);
    DataSet ds=new DataSet();
    date.Fill(ds,"date");
    string gettime=Convert.ToDateTime(ds.Tables["date"].Rows[0]["Date"]).ToString("yyyy-MM-dd hh:mm:ss");
    if (Convert.ToDateTime(gettime) > Convert.ToDateTime(current)) this.Label6.Text="Y";
      

  5.   


    Build报错:System.DateTime gettime=ds.Tables["date"].Rows[0]["Date"];
    Cannot implicitly convert type 'object' to 'System.DateTime'//System.DateTime gettime=ds.Tables["date"].Rows[0]["Date"];
    ds.Tables["date"].Rows[0]["Date"]它不能转换为时间类型,所以抱错