C#数据库访问:DBNull的VS空 [英] C# Database Access: DBNull vs null

查看:112
本文介绍了C#数据库访问:DBNull的VS空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有我们自己的ORM我们这里使用,并为我们所有的数据库表提供强类型的包装。我们还允许执行弱类型即席SQL,但是这些查询仍然通过相同的类获取值了数据读取的。

We have our own ORM we use here, and provide strongly typed wrappers for all of our db tables. We also allow weakly typed ad-hoc SQL to be executed, but these queries still go through the same class for getting values out of a data reader.

在调整该类与Oracle合作,我们已经遇到一个有趣的问题。它是更好地使用DBNull.Value,还是空?是否有使用DBNull.Value什么好处?这似乎更正确使用空,因为我们已经从数据库世界分别自己,但也有问题(你不能只是一味地的ToString()时,值为null为例),所以我们需要有绝对的东西做出有意识的决定。

In tweaking that class to work with Oracle, we've come across an interesting question. Is it better to use DBNull.Value, or null? Are there any benefits to using DBNull.Value? It seems more "correct" to use null, since we've separated ourselves from the DB world, but there are implications (you can't just blindly ToString() when a value is null for example) so its definitely something we need to make a conscious decision about.

推荐答案

我觉得最好使用空,而不是DB空。

I find it better to use null, instead of DB null.

究其原因是因为,如你所说,你从DB世界分离自己。

The reason is because, as you said, you're separating yourself from the DB world.

它通常是很好的做法来检查引用类型,以确保它们不为空反正。你将要检查空比DB数据其他东西,我觉得这是最好保持一致性,整个系统,并使用NULL,而不是的DBNull

It is generally good practice to check reference types to ensure they aren't null anyway. You're going to be checking for null for things other than DB data, and I find it is best to keep consistency across the system, and use null, not DBNull.

在从长远来看,我建筑发现它是更好的解决方案。

In the long run, architecturally I find it to be the better solution.

这篇关于C#数据库访问:DBNull的VS空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆