Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT / UPDATE data using DBNull or null?

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?

like image 771
Royi Namir Avatar asked Aug 14 '26 05:08

Royi Namir


1 Answers

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

like image 51
Yuck Avatar answered Aug 15 '26 22:08

Yuck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!