C# DataView 日期范围与 LIKE 运算符?

发布于 2024-07-28 22:08:41 字数 1847 浏览 9 评论 0原文

我有一个 XML 文件:

<SMS>
    <Number>+447761692278</Number>
    <DateTime>2009-07-27T15:20:32</DateTime>
    <Message>Yes</Message>
    <FollowedUpBy>Unassigned</FollowedUpBy>
    <Outcome></Outcome>
    <Quantity>0</Quantity>
    <Points>0</Points>
  </SMS>
  <SMS>
    <Number>+447706583066</Number>
    <DateTime>2009-07-27T15:19:16</DateTime>
    <Message>STOP</Message>
    <FollowedUpBy>Unassigned</FollowedUpBy>
    <Outcome></Outcome>
    <Quantity>0</Quantity>
    <Points>0</Points>
  </SMS>

我使用 XMLReader 将文件读入数据集并将其显示在 datagridview 中。 我希望能够指定显示数据的日期范围。 例如,元素包含 INSERT DATE 和 INSERT DATE 之间的日期。 插入日期。 为了做到这一点,我使用 DATAVIEW,然后用数据视图而不是数据集填充 datagridview。

目前我有一个方法如下:

public void DisplayRecSmsByDateRange(string date1,string date2, string readDir)
        {
            DataSet ds = new DataSet("SMS DataSet");
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml(readDir);
            ds = xmlDatadoc.DataSet;

            DataView custDV = new DataView(ds.Tables["SMS"]);
            custDV.RowFilter = String.Format("(DateTime >= DateTime LIKE '{0}*' AND DateTime <= DateTime LIKE '{1}*')", EscapeLikeValue(date1), EscapeLikeValue(date2));
            this.dataGridView1.DataSource = custDV;
        }

问题是,如果您查看 xml 文件,该元素包含时间和日期。 由于我对此元素的时间部分不感兴趣,因此我将使用“LIKE”语句仅根据元素的日期部分显示 xml 文件中的数据。 因此,当我尝试执行布尔运算例如“显示 2009-07-27 和 2009-07-30 之间日期的数据”时,我收到错误,因为编译器不喜欢我正在尝试将 LIKE 运算符与布尔 <=,>= 运算符结合起来(如上面的方法所示)。 有没有解决的办法? 我需要能够显示一系列日期之间的数据,但使用 LIKE 运算符仅基于元素的第一部分进行搜索。

非常感谢帮助,

亲切的问候。

I have an XML file:

<SMS>
    <Number>+447761692278</Number>
    <DateTime>2009-07-27T15:20:32</DateTime>
    <Message>Yes</Message>
    <FollowedUpBy>Unassigned</FollowedUpBy>
    <Outcome></Outcome>
    <Quantity>0</Quantity>
    <Points>0</Points>
  </SMS>
  <SMS>
    <Number>+447706583066</Number>
    <DateTime>2009-07-27T15:19:16</DateTime>
    <Message>STOP</Message>
    <FollowedUpBy>Unassigned</FollowedUpBy>
    <Outcome></Outcome>
    <Quantity>0</Quantity>
    <Points>0</Points>
  </SMS>

I use an XMLReader to read the file into a dataset and display it in a datagridview.
I want to be able to specify a range of dates to display the data for. For example where the element contains a date between INSERT DATE & INSERT DATE.
In order to do this I am using a DATAVIEW and then populating the datagridview with the dataview instead of the dataset.

Currently I have a method as below:

public void DisplayRecSmsByDateRange(string date1,string date2, string readDir)
        {
            DataSet ds = new DataSet("SMS DataSet");
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml(readDir);
            ds = xmlDatadoc.DataSet;

            DataView custDV = new DataView(ds.Tables["SMS"]);
            custDV.RowFilter = String.Format("(DateTime >= DateTime LIKE '{0}*' AND DateTime <= DateTime LIKE '{1}*')", EscapeLikeValue(date1), EscapeLikeValue(date2));
            this.dataGridView1.DataSource = custDV;
        }

The problem is that if you look at the xml file, the element contains the time as well as the date. Since I am not interested in the time part of this element, I would use the "LIKE" statement to display the data from the xml file based on just the date part of the element. As a result of this, when i try and perform a boolean operation to say for example - "Show me the data for dates between 2009-07-27 and 2009-07-30", I get an error since the compiler does not like that I am trying to combine the LIKE operator with the boolean <=,>= operators (Shown in the above method). Is there a way around this? I need to be able to display data between a range of dates but use the LIKE operator to search based only the first part of the element.

Help appreciated greatly,

Kind regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经大条 2024-08-04 22:08:41

我假设您可以修改/替换 EscapeLikeValue() 方法,以便它仅返回日期部分,例如“2009-09-27”。 这样你就可以将过滤器重写为:

custDV.RowFilter = String.Format("(DateTime >= '{0}' AND DateTime <='{1}')", EscapeLikeValue(date1) + "T00:00:00", EscapeLikeValue(date2) + "T23:59:59");

HTH,
德扬

I'll assume that you can modify/replace the EscapeLikeValue() method so that it returns only date part, e.g. "2009-09-27". This way you can rewrite the filter to something like:

custDV.RowFilter = String.Format("(DateTime >= '{0}' AND DateTime <='{1}')", EscapeLikeValue(date1) + "T00:00:00", EscapeLikeValue(date2) + "T23:59:59");

HTH,
Dejan

命比纸薄 2024-08-04 22:08:41

我想知道您是否可以执行以下操作:

custDV.RowFilter = String.Format("(Convert(DateTime,'System.DateTime' >= #{0}# AND Convert(DateTime,'System.DateTime' <= #{1}#)", EscapeLikeValue(date1), EscapeLikeValue(date2));

I wonder if you can do the following:

custDV.RowFilter = String.Format("(Convert(DateTime,'System.DateTime' >= #{0}# AND Convert(DateTime,'System.DateTime' <= #{1}#)", EscapeLikeValue(date1), EscapeLikeValue(date2));
碍人泪离人颜 2024-08-04 22:08:41

执行此操作的一种方法是将日期字符串参数转换为实际的日期时间对象,如下所示

 DateTime dt = System.Convert.ToDateTime(date1);
 DateTime dt2 = System.Convert.ToDateTime(date2);
 dt2=dt2.AddDays(1);

,然后您可以像这样修改您的位置

custDV.RowFilter=String.Format("DateTime>={0} and DateTime<{1}",dt.ToShortDateString(),dt2.ToShortDateString());

One way you can do this is to convert the date string parameters into actual datetime objects like so

 DateTime dt = System.Convert.ToDateTime(date1);
 DateTime dt2 = System.Convert.ToDateTime(date2);
 dt2=dt2.AddDays(1);

and then you can modify your where like so

custDV.RowFilter=String.Format("DateTime>={0} and DateTime<{1}",dt.ToShortDateString(),dt2.ToShortDateString());
凤舞天涯 2024-08-04 22:08:41

对方法稍作修改,删除 LIKE 运算符就起作用了......

public void DisplayRecSmsByDateRange(string date1,string date2, string readDir)
            {
                DataSet ds = new DataSet("SMS DataSet");
                XmlDataDocument xmlDatadoc = new XmlDataDocument();
                xmlDatadoc.DataSet.ReadXml(readDir);
                ds = xmlDatadoc.DataSet;

                DataView custDV = new DataView(ds.Tables["SMS"]);
                custDV.RowFilter = String.Format("(DateTime >= '{0}*' and DateTime <= '{1}*')", EscapeLikeValue(date1), EscapeLikeValue(date2));

                custDV.Sort = "DateTime";
                this.dataGridView1.DataSource = custDV;
            }

A slight alteration to the method, removing the LIKE operator worked......

public void DisplayRecSmsByDateRange(string date1,string date2, string readDir)
            {
                DataSet ds = new DataSet("SMS DataSet");
                XmlDataDocument xmlDatadoc = new XmlDataDocument();
                xmlDatadoc.DataSet.ReadXml(readDir);
                ds = xmlDatadoc.DataSet;

                DataView custDV = new DataView(ds.Tables["SMS"]);
                custDV.RowFilter = String.Format("(DateTime >= '{0}*' and DateTime <= '{1}*')", EscapeLikeValue(date1), EscapeLikeValue(date2));

                custDV.Sort = "DateTime";
                this.dataGridView1.DataSource = custDV;
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文