简体   繁体   中英

Passing NULL Value to Stored Procedure Through C#

I have an update class in my MVC Web App to update a field from the front end. This calls an procedure on the database which updates the cell in the table. (Note: This column can accept NULL values)

If I set this in my C# code then it work as it checks to see if a NULL is passed in the front end and replaces it with an empty string (a space could also work):

update.Value = dataModel.Value == null ? string.Empty : dataModel.Value;

However, when I query the database table, it's not setting NULL value in the cell but just a blank. How can I pass a NULL value into the procedure through the C# code?

You can use DBNull.Value to pass Null value in database. This is .Net substitue of null value of DB.

So your code becomes

update.Value = dataModel.Value == null ? DBNull.Value : dataModel.Value;

https://msdn.microsoft.com/en-us/library/system.dbnull.value(v=vs.110).aspx

尝试使用此command.Parameters.AddWithValue("@param", DBNull.Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM