简体   繁体   English

选择关键字require>而不是=

[英]Select keyword require > instead of =

if i use = instead of > in line below TextBox1.Text = "regdate > '12/12/2012 8:27:09'"; 如果我在TextBox1.Text下面的行中使用=而不是>,则为“ regdate> '12 / 12/2012 8:27:09'”;

it return me zero rows . 它返回我零行。 Any reason ? 任何原因 ?

        DataView dv1 = new DataView ();
        DataSet ds = new DataSet ();
        DataTable dt = new DataTable();

        /*
         col1   col2            col3    col4            col5
         1      sw@yahoo.com    sw      Stephen Walther 12/12/2012 8:27:09 PM
         2      as@yahoo.com    as      Al Stevens      12/12/2012 8:27:09 PM
        */

// Here is the code and criteria for selecting few rows based on datetime . //这是用于基于datetime选择几行的代码和标准。

        TextBox1.Text = "regdate > '12/12/2012'";

        //connection created 

        SqlConnection con = new SqlConnection(SqlDataSource1.ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter ("SELECT * FROM users",con);
        da.Fill (ds);

        // filtering criteria applied .
        DataRow[] dr = ds.Tables["Table"].Select(TextBox1.Text);

         //columns created
        dt.Columns.Add("col1", typeof(int));
        dt.Columns.Add("col2", typeof(string));
        dt.Columns.Add("col3", typeof(string));
        dt.Columns.Add("col4", typeof(string));
        dt.Columns.Add("col5", typeof(DateTime));

         // data added
        foreach (DataRow item in dr)
        {
            dt.Rows.Add(item.ItemArray);
        }

        // view created
        dv1 = dt.DefaultView ;
        GridView1.DataSourceID = string.Empty; 
        GridView1.DataSource = dv1 ;
        Page.DataBind(); 

Because the time component is stopping the match - it is trying to match the date AND time. 由于时间部分正在停止比赛-试图匹配日期和时间。

You could try to use some kind of DATEDIFF function to just match the date portion 您可以尝试使用某种DATEDIFF函数来匹配日期部分

How to test two datetimes for equality in T-SQL (ignoring their time components)? 如何在T-SQL中测试两个日期时间是否相等(忽略它们的时间成分)?

BUT - when amending the code just be really careful with this - not to start putting text box input into the SQL statement. 但是-修改代码时要特别小心-不要开始将文本框输入放到SQL语句中。 Injection attack risk aplenty. 注射攻击风险充沛。

I think you are OK as it stands but.... 我认为您现在还可以,但是....

Because it's a datetime field, so it will compare the whole date and time value, not just the date. 因为它是一个datetime字段,所以它将比较整个日期和时间值,而不仅仅是日期。

The value 12/12/2012 8:27:09 PM and 12/12/2012 are not the same, because the 12/12/2012 gets expanded to the 12/12/2012 12:00:00 AM value to be comparable to the datetime value. 12/12/2012 8:27:09 PM12/12/2012并不相同,因为12/12/2012被扩展为12/12/2012 12:00:00 AM值到datetime值。 The datetime value doesn't get truncated to be comparable to the date value. datetime值不会被截断以与日期值相当。

Generally when data types doesn't match for a comparison or calculation, the smaller type gets expanded to the larger type, so that there is no unintentional data loss. 通常,当数据类型不适合进行比较或计算时,较小的类型将扩展为较大的类型,这样就不会造成无意的数据丢失。

The database stores the date in (about) millisecond resolution. 数据库以(大约)毫秒的分辨率存储日期。 When "12/12/2012 8:27:09 PM" is displayed, there is probably a millisecond part that is not displayed, but will throw off a comparison with a date that doesn't specify those milliseconds. 当显示“ 12/12/2012 8:27:09 PM”时,可能会显示一个毫秒部分,但该日期与未指定这些毫秒的日期进行比较。

The ">" comparison will succeed because the stored date is higher than your specified date, precisely because of those milliseconds. 在“>”比较会成功,因为存储的日期因为那些毫秒的精确比您指定的日期较高。

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

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