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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With