从数据库中获取字符串中的日期

时间:2014-05-07 05:12:09

标签: c# mysql

我正在尝试将数据库中的日期转换为字符串,并将其与今天的日期进行比较以执行某些操作。  我作为解决方案做了什么,但标签仍然没有显示消息。

                      if (FileUpload1.PostedFile != null)
    {
        string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

        //Save files to disk
        FileUpload1.SaveAs(Server.MapPath("Files/" + FileName));
        string FilePath = "Files/" + FileName;
        //SqlCommand cmd = new SqlCommand();
        DAL obj = new DAL();
        using (SqlConnection conn = obj.openCon())
        {
            String sql = "Select DueDate from tbl_AssignmentUpload1 where AssignmentTitle like '" + AssignmentTitle + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            DateTime duedate = new DateTime() ;


            if (dr != null && dr.HasRows)
            {
                while (dr.Read())
                {
                    duedate = dr.GetDateTime(0);
                }
                dr.Close();

                // now check if today greater than due date and update
                if (duedate != null && today.Date > duedate)
                {
                    sql = "Insert into tbl_AssignmentSubmit( Name ,AridNumber, Shift , Degree , Course , FileName ,FilePath ) values ('" + txt_Name.Text + "' , '" + txt_AridNumber.Text + "', '" + shift + "', '" + Degree + "', '" + Course + "','" + FileName + "','" + FilePath + "')";
                    cmd = new SqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    lbl_uploaded.Text = "Assignment can not be Submitted.You crossed the due date.";
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您必须从DueDate获取tbl_AssignmentUpload1。 例如:

string strSQL = "Select DueDate from tbl_AssignmentUpload1 where AssignmentTitle like          @AssignmentTitle ";
(SqlCommand myCommand = new SqlCommand(strSQL, cnn)) // Cnn is your sql connection
{ 
  myCommand.Parameters.AddWithValue("@AssignmentTitle", AssignmentTitle );
  using (SqlDataReader reader = myCommand.ExecuteReader())
  {
    while (reader.Read())
    {
     DateTime today1 = Convert.ToDateTime(reader["DueDate "].ToString());
    }
  }
}

在此之后,您可以执行插入语句

答案 1 :(得分:0)

尝试这样的事情。好了,更新了duedate的修复程序。

                using (SqlConnection conn = SQL.GeSQLConnection())
                {
                    String sql = "Select DueDate from tbl_AssignmentUpload1 where AssignmentTitle like '" + AssignmentTitle + "'";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlDataReader dr = cmd.ExecuteReader();
                    DateTime duedate = new DateTime();
                    if (dr != null && dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            duedate = dr.GetDateTime(0);
                        }
                        dr.Close();

                        // now check if today greater than due date and update
                       if(duedate != null && DateTime.Today > duedate)
                       {
                        sql = "Insert into tbl_AssignmentSubmit( Name ,AridNumber, Shift , Degree , Course , FileName ,FilePath ) values ('" + txt_Name.Text + "' , '" + txt_AridNumber.Text + "', '" + shift +"', '" + Degree + "', '" + Course + "','" + FileName + "','" + FilePath + "')";
                        cmd = new SqlCommand(sql, conn);
                        cmd.ExecuteNonQuery();
                      }
                      else
                      {
                        lbl_uploaded.Text = "Assignment can not be Submitted.You crossed the due date.";
                      }
                    }
                    else
                      {
                        lbl_uploaded.Text = "No Due date was selected for the given assesment title";
                      }
                }