简体   繁体   English

字符串日期比较C#

[英]String date comparison C#

I have an application that pulls a date from a MySQL database (string format: 01-jan-2000). 我有一个从MySQL数据库提取日期的应用程序(字符串格式:2000年1月1日)。 I need to compare this data with the date entered in a TextBox. 我需要将此数据与在TextBox中输入的日期进行比较。

This is what i have so far: 这是我到目前为止所拥有的:

String query = "select * from tb_demographic_data where date_of_birth LIKE '%" +
       txb_startDate.Text + "' OR (str_to_date(date_of_birth,'%d,%b,%Y') <= 
       str_to_date('"+txb_startDate.Text+"','%d,%b,%Y'))

When I run the query I get no results. 当我运行查询时,没有任何结果。 Nothing is wrong with my sql code. 我的sql代码没有问题。 I think the error occurs when i change the string to a date with str_to_date() . 我认为使用str_to_date()将字符串更改为日期时会发生错误。 Can anyone help? 有人可以帮忙吗?

Don't do string comparisons of dates. 不要对日期进行字符串比较。 Change the column in the database to be a date, and update your query to accept a date value using a parametrized statement or similar. 将数据库中的列更改为日期,并使用参数化语句或类似方法更新查询以接受日期值。

It looks like your date format is wrong... '%d,%b,%Y' is saying that the separators are commas. 看来您的日期格式有误... '%d,%b,%Y'表示分隔符为逗号。 I would imagine you want something like '%d-%b-%Y' . 我可以想象你想要像'%d-%b-%Y'

As Nikhil pointed out, having a user enter a date in a date/calendar/clock control is typically a much better choice. 正如Nikhil指出的那样,让用户在日期/日历/时钟控件中输入日期通常是更好的选择。 In addition, I would highly recommend not taking user input from a textbox and simply pasting it inside a SQL string. 另外,我强烈建议不要从文本框中获取用户输入,而只是将其粘贴到SQL字符串中。 This leads to SQL injection . 这导致SQL 注入

I would recommend that you either use one of the above recommended controls to have users specify a specific DateTime , or convert the data in your text box to a DateTime object . 我建议您或者使用上面推荐的控件之一来让用户指定特定的DateTime ,或者将文本框中的数据转换为DateTime对象 Assuming you are using DbCommand objects to run your query, you can then use a parameterized query and pass in your DateTime as a parameter, rather than directly putting it into the command. 假设您正在使用DbCommand对象运行查询,然后可以使用参数化查询并将DateTime作为参数传递,而不是直接将其放入命令中。 Your SQL could look like the following: 您的SQL可能如下所示:

select * from tb_demographic_data
where date_of_birth <= @startDate

You would then have to add a DbParameter to your DbCommand with a ParameterName of "@startDate" and a Value of your DateTime object. 然后,您必须在DbCommand添加一个ParameterName"@startDate"DbParameter并添加一个DateTime对象的Value

i figured it out. 我想到了。 I'm sure its not the most efficient way to do this but I'm fairly new to this. 我确信这不是执行此操作的最有效方法,但是我对此并不陌生。 Here it is: I just parsed the two strings as dates and calculated the date difference between them. 在这里:我只是将两个字符串解析为日期,然后计算了它们之间的日期差。 If the answer is positive then the first date comes before the second and vice versa 如果答案是肯定的,则第一个日期在第二个日期之前,反之亦然

select * from tb_demographic_data 
where date_of_birth 
    LIKE '%" + txb_startDate.Text + "' 
    OR (datediff(str_to_date('" + txb_startDate.Text + "', 
                             '%d-%b-%Y'), 
                 str_to_date(date_of_birth,
                             '%d-%b-%Y'))>=0) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM