C# DataView 日期范围与 LIKE 运算符?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您可以修改/替换 EscapeLikeValue() 方法,以便它仅返回日期部分,例如“2009-09-27”。 这样你就可以将过滤器重写为:
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:
HTH,
Dejan
我想知道您是否可以执行以下操作:
I wonder if you can do the following:
执行此操作的一种方法是将日期字符串参数转换为实际的日期时间对象,如下所示
,然后您可以像这样修改您的位置
One way you can do this is to convert the date string parameters into actual datetime objects like so
and then you can modify your where like so
对方法稍作修改,删除 LIKE 运算符就起作用了......
A slight alteration to the method, removing the LIKE operator worked......