Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以字符串形式从数据库获取日期_C#_Mysql - Fatal编程技术网

C# 以字符串形式从数据库获取日期

C# 以字符串形式从数据库获取日期,c#,mysql,C#,Mysql,我试图将数据库中的日期转换成字符串,并将其和今天的日期进行比较,以执行一些操作。 我做了什么作为解决方案,但标签仍然没有显示消息 if (FileUpload1.PostedFile != null) { string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); //Save files to disk FileUp

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

                      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.";
                }
            }
        }
    }

您必须从
tbl\u分配上传1
获取
DueDate
。 例如:

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());
    }
  }
}

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

尝试类似的操作。Ok更新了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";
                      }
                }

查看错误是什么会很有用:)您是否意识到
date
变量实际上不包含日期?字符串未被识别为有效的日期时间。有一个从索引0开始的未知单词。我想知道如何从数据库中检索日期并进一步使用它。@SriramSakthivel是的。我不知道如何处理。为什么在这里使用myCommand.AddWithValue(“@userID”,user)?最后一行给出了系统无效强制转换异常。程序在这一行给出了错误。表示无法将null转换为dateTime格式。DateTime duedate=null@user2444546只需将其设为DateTime duedate=new DateTime();查看更新的代码。代码运行正常,没有任何错误,但标签不显示文本。要么提交,要么不提交。这是代码,看看我是否犯了什么错误。你的意思是select查询没有结果吗?请查看更新的代码。是的。虽然数据库包含值。