处理数据库中的 null
我需要四舍五入并打印如下所示的价格,但这不处理空值。
如何在一行代码上处理空值?
DBRSet是一个SQLDataReader,价格是SQL中的money类型。
<%= Math.Round(DBRSet("price"))%>
我有大约 200 个 .aspx 页面,所以我还可以使用一些关于如何以简单的方式更改这些页面的提示?我可以进行搜索并用正则表达式或类似的东西替换吗?
I need to round and print a price like below, but this does not handle null values.
How can I handle null values on one line of code?
DBRSet is an SQLDataReader and the price is money type in SQL.
<%= Math.Round(DBRSet("price"))%>
I have about 200 .aspx pages of this so I could also use some tips on how to change these in an easy way? Can I do a search and replace with a reg-exp or something like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须显式处理
DbNull
情况,例如:这很笨拙,因此在某处有一个类似这样的辅助方法是有意义的:
这将允许
当然找到并更改所有要使用的此类代码辅助方法会......令人不愉快。我会通过在整个项目(可能在项目的较小块中)搜索一些指示性内容(可能是 Math.Round?)并在替换之前手动检查它来完成此操作。
You have to handle the
DbNull
case explicitly, for example:This is unwieldy, so it makes sense to have a helper method something like this somewhere:
Which would allow
Of course finding and changing all such code to use the helper method would be... unpleasant. I would do it by searching throughout the project (maybe in smaller chunks of the project) for something indicative (maybe
Math.Round
?) and review it manually before replacing.如果您不想更改 aspx,但可以轻松更改
DBRSet
属性的定义,则可以在SqlDataReader
上放置一个包装器并实现您自己的索引器首先检查是否为 null,然后进入内部dataReader
以获取值(如果不为 null)。If you don't want to change the aspx's, but can easily change the definition of the
DBRSet
property, you can put there a wrapper over theSqlDataReader
and implement your own indexer that would first check for null and go into the innerdataReader
to get the value if not null.