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,然后用 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 年 7 月 27 日和 2009 年 7 月 30 日之间日期的数据”时,我收到一个错误,因为编译器不喜欢我正在尝试将 LIKE 运算符与布尔 <=,>= 运算符结合起来(如上述方法所示)。有没有解决的办法?我需要能够显示日期范围之间的数据,但使用 LIKE 运算符仅基于元素的第一部分进行搜索。

非常感谢您的帮助,

亲切的问候。

4

4 回答 4

0

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

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

HTH,德让

于 2009-07-28T13:44:30.070 回答
0

您可以这样做的一种方法是将日期字符串参数转换为实际的日期时间对象,如下所示

 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());
于 2009-07-28T13:45:47.993 回答
0

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

custDV.RowFilter = String.Format("(Convert(DateTime,'System.DateTime' >= #{0}# AND Convert(DateTime,'System.DateTime' <= #{1}#)", EscapeLikeValue(date1), EscapeLikeValue(date2));
于 2009-07-28T13:46:55.247 回答
0

对方法稍作改动,去掉 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;
            }
于 2009-07-28T14:02:28.800 回答