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:
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.Valueand not anullreference.Although it doesn’t matter which we use to
INSERTorUPDATEdata, I would tend to stick withDBNull.Valuefor consistency through data access code.There are a variety of other opinions in the question C# Database Access: DBNull vs null