标记中的 NullDisplayText 与 SQL 中的 ISNULL(field, 0) ?
使用哪种方法更好:
- 未设置
BoundField.NullDisplayText
。 SQL 查询中预见到 NULL 情况,即
SELECT ISNULL(amount, 0) FROM table
或
- 设置
BoundField.NullDisplayText
,例如“0.00 %”。 SQL 查询中没有预见到 NULL 情况,即SELECT amount FROM table
您觉得怎么样?
Which approach is better to use:
BoundField.NullDisplayText
isn't set. NULL-case is foreseen in SQL query, i.e.SELECT ISNULL(amount, 0) FROM table
or
BoundField.NullDisplayText
is set, e.g. "0.00 %". NULL-case isn't foreseen in SQL query, i.e.SELECT amount FROM table
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
显然是第一个,因为您使用 ISNULL 进行过滤。
Clearly the first one, because you filter with ISNULL.
我认为第二个选择更好。通常,在中间层或表示层而不是数据库中格式化输出更好。因此,我希望将空值返回到数据层代码之上的层,并让它决定如何显示,而不是在数据库中做出选择。
通过将空值转换为零,您可以向所有使用查询的系统声明空值等同于用户故意输入零。如果确实如此,那么可以使用 Coalesce 而不是 IsNull 并将 null 转换为零。但是,如果即使存在极小的可能性,即查询将被重用,并且缺少值的处理方式可能与输入零不同,那么我将向中间层返回空值,并让它决定如何处理它。
I would argue that the second choice is better. It is generally better to format output in the middle-tier or presentation tier rather than the database. Thus, I would want to return nulls to the tier above the data tier code and have it decide what to do about display rather than make the choice at the database.
By converting nulls to zeros, you are stating for all systems that use the query that a null equates to a user intentionally entering a zero. If that is actually the case, then fine, use Coalesce instead of IsNull and convert nulls to zeroes. However, if there is even the remotest possibility that the query will be reused and that the absence of a value might be treated differently than the entry of a zero, I would return nulls to the middle tier and let it decide what to do about it.
你能两全其美吗?
Can you do best of both?
区别在于 0.00 表示该字段有值,而 NULL 表示相反。因此,从数据健全性的角度来看,前者是正确的。
The difference is that 0.00 implies that the field has a value, whereas NULL implies that the opposite. From a data sanity perspective, therefore, the former is correct.