比较日期时间与字符串 [英] compare datetime with string

查看:100
本文介绍了比较日期时间与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从数据库中填充datagridview,但是将数据库中的字符串与日期进行比较. 这是我想做的事情:

例如,在数据库中,我有一列的值为"19.03.2012"(作为字符串类型,我尝试设置日期时间类型,但未成功),并且我想选择比"10.10"大的值. .2010",并用指定的值填充数据网格

这是我用来填充数据网格的代码(我知道为什么此代码无法工作,只是让您知道我想做什么):

Hi, what i want to do is to populate a datagridview from a database, but with comparing a string from the database with a date..
Here is what i wanna do:

in the database for example i have a column with the value "19.03.2012" (as a string type, i tried to make the datetime type but no success), and i want to select the values that are greater then for example "10.10.2010" and fill the datagrid with the specified value

here is the code where i call to populate the datagrid (i am aware why this code wont work, it is just so you know what i want to do):

private void PopulateDataGrid()
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM TableExample WHERE Date > '10.10.2010'", conn);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);

            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            da.Fill(table);
            TableExampleBindingSource.DataSource = table;

            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = TableExampleBindingSource;
        }



那么,当我想比较日期时,该如何使用更大或更小的数字呢?

感谢前进...



So how can i use greater or less then, when i want to compare dates?

Thanks in forward...

推荐答案

你好

不好的方法!

如果使用的是SQL Server,请使用DateTime而不是nvarchar.

顺便说一句:
如果要并坚持使用字符串类型作为日期,请更改格式.例如:
"20120123"而不是"01.23.2012"

Hello

Bad way!

If you are using SQL Server, use DateTime instead of nvarchar.

By the way:
If you want and insist to use string type for date, then change your format. For example:
"20120123" instead of "01.23.2012"

SELECT * FROM TableExample WHERE [Date] > '20101010'


这既快捷又方便脏的,我不推荐.哈哈!我也在这里做很多假设...尝试一个例子:

SELECT CONVERT(DATETIME,``19.03.2010'',103)

这会将您的欧洲日期/字符串转换为美国格式.因此,您可以执行以下操作:

SELECT * FROM TableExample WHERE Date> CONVERT(DATETIME,``19.03.2010'',103)

如果问题在于日期可以采用任意多种格式,那么一旦将数据放入dataTable中然后再进行表格处理,您可能会考虑运行更清洁的方法.

另一个选择是清除SQL中的数据并更改接口以仅接受某些日期/格式.如果您给用户提供输入废话的机会,他们会的. :)
This is quick and dirty, and I don''t recommend it. hah! I''m also making a lot of assumptions here... Try this out for an example:

SELECT CONVERT(DATETIME, ''19.03.2010'', 103)

This will convert your European date/string to the american format. So then you can do this:

SELECT * FROM TableExample WHERE Date > CONVERT(DATETIME, ''19.03.2010'', 103)

If the problem is that the date could be in any number of formats, then you might think about running a cleaner once you have the data in the dataTable and then doing a table.Select.

The other option would be to clean the data in SQL and change the interface to only accept certain dates/formats. If you give users the chance to enter crap, they will. :)


这篇关于比较日期时间与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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