如何通过参数化查询在数据库中插入空值我有一个datetime数据类型:dttm数据库字段类型也是datatime现在我这样做:如果(dttm.HasValue){cmd.Parameters.AddWithValue("@dtb",dttm);}else{//它应该通过cmd.Parameters.AddWithValue("@dtb",_____)将空值插入数据库//}如何做到这一点。使用DBNull.Valueif(dttm.HasValue){cmd.Parameters.AddWithValue("@dtb",dttm);}else{cmd.Parameters.AddWithValue("@dtb",DBNull.Value)}这可以使用空合并运算符:如果dttm的值为null,将插入DBNull.Value,否则将使用dttm的值cmd.Parameters.AddWithValue("@dtb",dttm??(object)DBNull.Value);这将消除对if语句的需要如果您的字段允许空值;以上就是C#学习教程:如何通过参数化查询在数据库中插入空值分享全部内容,如果对大家有用又需要详细了解C#学习教程,希望大家多多指教多加注意—if(dttm.HasValue){cmd.Parameters.AddWithValue("@dtb",dttm);}else{cmd.Parameters.AddWithValue("@dtb",DBNull.Value)}本文来自网络合集,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
