INSERT / UPDATE data using DBNull or null?

淺唱寂寞╮ 提交于 2019-12-23 17:51:56

问题


When modifying data in a SQL Server database you can use either System.DBNull.Value or null to represent a NULL value. Both of these will work and will set the proper value to NULL.

My question is - which of these is preferred, and why? Are there certain cases where one should be used in place of the other?


回答1:


From System.DBNull.Value != null, and by testing this code:

var result = (System.DBNull.Value == null); // this is always false

We can see that the CLR doesn't treat them the same way. When retrieving data from a database we need to check for DBNull.Value and not a null reference.

Although it doesn't matter which we use to INSERT or UPDATE data, I would tend to stick with DBNull.Value for consistency through data access code.

There are a variety of other opinions in the question C# Database Access: DBNull vs null




回答2:


DBNull is representative of a variant and is not type-specific. In a general context, I would expect a performance hit anywhere you have a construct that expects a strongly-typed dataset and has to convert DBNull to a "typed-NULL".



来源:https://stackoverflow.com/questions/7917921/insert-update-data-using-dbnull-or-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!