如何在C#中将当前日期与数据库现有日期进行比较 [英] How to compare current date with database existing date in C#

查看:343
本文介绍了如何在C#中将当前日期与数据库现有日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将当前日期与c#中的数据库现有日期进行比较。



我尝试了什么:



< pre> 
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString);

string userid = GVmydsr.DataKeys [e.RowIndex] .Value.ToString();
GridViewRow row =(GridViewRow)GVmydsr.Rows [e.RowIndex];

//字符串日期= GVmydsr.Rows [e.RowIndex] .Cells [1] .Text;
标签日期=(标签)row.FindControl(lblcurrentdate);



GVmydsr.EditIndex = -1;

DropDownList ddl1 = GVmydsr.Rows [e.RowIndex] .FindControl(ddlclient)as DropDownList;
TextBox txt1 = GVmydsr.Rows [e.RowIndex] .FindControl(txtarea)as TextBox;
TextBox txt2 = GVmydsr.Rows [e.RowIndex] .FindControl(txtcmpnyname)as TextBox;
TextBox txt3 = GVmydsr.Rows [e.RowIndex] .FindControl(txtcname)as TextBox;
TextBox txt4 = GVmydsr.Rows [e.RowIndex] .FindControl(txtmobile)as TextBox;
TextBox txt5 = GVmydsr.Rows [e.RowIndex] .FindControl(txtemail)as TextBox;
TextBox txt6 = GVmydsr.Rows [e.RowIndex] .FindControl(txtaddr)as TextBox;
TextBox txt7 = GVmydsr.Rows [e.RowIndex] .FindControl(txtland)as TextBox;
TextBox txt8 = GVmydsr.Rows [e.RowIndex] .FindControl(txtpresence)as TextBox;
DropDownList ddl2 = GVmydsr.Rows [e.RowIndex] .FindControl(ddlstatus)as DropDownList;
DropDownList ddl3 = GVmydsr.Rows [e.RowIndex] .FindControl(ddlresult)as DropDownList;
TextBox txt9 = GVmydsr.Rows [e.RowIndex] .FindControl(txtcomment)as TextBox;

string day = DateTime.Today.ToShortDateString()。ToString();
if(date.ToString()== day.ToString())
{
con.Open();
SqlCommand cmd = new SqlCommand(update [dsr_data] set client_type ='+ ddl1.Text +',area ='+ txt1.Text +',co​​mpany_name ='+ txt2.Text + ',client_name ='+ txt3.Text +',mobile ='+ txt4.Text +',email ='+ txt5.Text +',address_name ='+ txt6.Text +', landmark ='+ txt7.Text +',curr_presence_appoint ='+ txt8.Text +',status_type ='+ ddl2.Text +',result ='+ ddl3.Text +',comment = '+ txt9.Text +'其中id ='+ userid +',con);
cmd.ExecuteNonQuery();
con.Close();

gvbind();
}
}

解决方案

你可以试试像

  string  day = DateTime.Today.ToString(  MM / DD / YYYY); 

if (date.ToString( MM / dd / yyyy)== day)
{
// 做一些事情
}



顺便说一句,如果可能的话,尝试使用参数化查询以避免潜在的SQL注入。以下是使用参数化查询的示例防止SQL Server中的SQL注入攻击 [ ^ ]


将当前日期与数据库中的日期进行比较 [ ^ ]


首先关闭,制作它习惯在 [ ^ ]使用Statement来确保对象将在使用后正确处理和关闭。



其次,请勿将输入字段的值附加到SQL查询中,因为它可能会引导您到SQL注入攻击。请改用参数化查询。请参阅:保护您的数据:防止SQL注入 [ ^ ]



现在回到原来的问题。比较字符串日期非常简单,如下所示:



 如果 (Convert.ToDateTime(date).Equals(DateTime.Now())){
// 做东西
}





您需要比较 DateTime object而不是字符串。您只需确保Label中日期的值是有效的日期时间,或者使用 DateTime.TryParse()而不是转换。 ToDateTime()


I am not able to compare current date with database existing date in c#.

What I have tried:

<pre>
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        string userid = GVmydsr.DataKeys[e.RowIndex].Value.ToString();
        GridViewRow row = (GridViewRow)GVmydsr.Rows[e.RowIndex];

        //string date = GVmydsr.Rows[e.RowIndex].Cells[1].Text;
        Label date = (Label)row.FindControl("lblcurrentdate");
        
        

        GVmydsr.EditIndex = -1;

        DropDownList ddl1 = GVmydsr.Rows[e.RowIndex].FindControl("ddlclient") as DropDownList;
        TextBox txt1 = GVmydsr.Rows[e.RowIndex].FindControl("txtarea") as TextBox;
        TextBox txt2 = GVmydsr.Rows[e.RowIndex].FindControl("txtcmpnyname") as TextBox;
        TextBox txt3 = GVmydsr.Rows[e.RowIndex].FindControl("txtcname") as TextBox;
        TextBox txt4 = GVmydsr.Rows[e.RowIndex].FindControl("txtmobile") as TextBox;
        TextBox txt5 = GVmydsr.Rows[e.RowIndex].FindControl("txtemail") as TextBox;
        TextBox txt6 = GVmydsr.Rows[e.RowIndex].FindControl("txtaddr") as TextBox;
        TextBox txt7 = GVmydsr.Rows[e.RowIndex].FindControl("txtland") as TextBox;
        TextBox txt8 = GVmydsr.Rows[e.RowIndex].FindControl("txtpresence") as TextBox;
        DropDownList ddl2 = GVmydsr.Rows[e.RowIndex].FindControl("ddlstatus") as DropDownList;
        DropDownList ddl3 = GVmydsr.Rows[e.RowIndex].FindControl("ddlresult") as DropDownList;
        TextBox txt9 = GVmydsr.Rows[e.RowIndex].FindControl("txtcomment") as TextBox;

        string day = DateTime.Today.ToShortDateString().ToString();
        if (date.ToString() == day.ToString())
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("update [dsr_data] set client_type='" + ddl1.Text + "' ,area='" + txt1.Text + "',company_name='" + txt2.Text + "',client_name='" + txt3.Text + "',mobile='" + txt4.Text + "',email='" + txt5.Text + "',address_name='" + txt6.Text + "',landmark='" + txt7.Text + "',curr_presence_appoint='" + txt8.Text + "',status_type='" + ddl2.Text + "', result='" + ddl3.Text + "', comment='" + txt9.Text + "' where id='" + userid + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();

            gvbind();
        }
    }

解决方案

You can try something like

string day = DateTime.Today.ToString("MM/dd/yyyy");

if (date.ToString("MM/dd/yyyy") == day)
{
    //do something
}


By the way, try utilized parameterized query if possible to avoid potential SQL injection. Here is an example Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]


compare current date with a date in database[^]


First off, make it a habit to put objects that eat resources such as SqlConnection, SqlCommand and SqlDataAdapter within a [^]using Statement to ensure that objects will be properly disposed and closed after they are used.

Second, "DO NOT" append the value of your input fields to your SQL query as it can lead you to SQL Injection attack. Use parameterize query instead.See: Protect Your Data: Prevent SQL Injection[^]

Now back to your original question. Comparing string date is very straight-forward like this:

if (Convert.ToDateTime(date).Equals(DateTime.Now())){
  //do stuff
}



You need to compare a DateTime object and not a string. You just need to make sure that the value of the date from your Label is a valid datetime or else use DateTime.TryParse() instead of Convert.ToDateTime()


这篇关于如何在C#中将当前日期与数据库现有日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆