空值转换为“默认"值. [英] Null value converted into "default"

查看:93
本文介绍了空值转换为“默认"值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cmd.ExecuteNonQuery()上使用C#4.0;在SQL Server配置文件的帮助下出现错误,我看到了:

i am using C# 4.0 at cmd.ExecuteNonQuery(); there is an error with the help of SQL server profile i saw that :

cmd.Parameters.Add("@employeeSocialTitle", SqlDbType.Char, 5).Value = employeeSocialTitle;



当employeeSocialTitle为null时,将其替换为 默认值
这会导致错误,因此当employeeSocialTitle值为null时,如何强制其插入null

考虑到



when employeeSocialTitle is null it replace it by default
and that makes error so how can i force it to insert null when the employeeSocialTitle value is null

regards

推荐答案

我认为,由于 SQL server期望使用DBNull.Value而不是null,因此当employeeSocialTitle为null时,它将替换为default.为避免这种情况,可以按以下方式使用null合并运算符:
I think since the SQL server expects DBNull.Value instead of null, when employeeSocialTitle is null, it is replaced with default. To avoid this the null coalescing operator can be used as follows:
cmd.Parameters.Add("@employeeSocialTitle", SqlDbType.Char, 5).Value = employeeSocialTitle ?? DBNull.Value;


这篇关于空值转换为“默认"值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆