问题
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