且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在数据表中定义则DateDiff为计算列

更新时间:2023-12-01 18:29:52

麻HANIN指出,在他的评论,看起来则DateDiff不能在的 DataColumn的前pressions 的。你可以尝试建立计算列到下面的表中,而不是(如果你使用的是MS SQL或类似)

As M.A Hanin pointed out in his comment, it looks like DateDiff cannot be used in DataColumn Expressions. You could try building the calculated column into the underlying table instead (if you are using MS Sql or similar)

编辑: 有没有函数来获得'今天',但假设您要添加仅几个小时存在的DataColumn,你可以建立在今天的日期为常数,然后用比较操作而不是则DateDiff

edit: There is no function to get 'today', but assuming that the DataColumn you are adding will only exist for a few hours, you could build in todays date as a constant, and then use comparison operators instead of DateDiff

试试这个:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string),
    String.Format("IIF(DateTimeExpired > #{0}#,'No','Yes')",
    DateTime.Now.ToString("dd/MMM/yyyy")));

请注意,这会出现您的DataColumn只保留在内存中的不到一天的时间才有效。

Note this will only work if your DataColumn is only retained in memory for less than a day.