我有一个可以为空的DateTime变量.我想把它写到SQL DB.当我尝试插入时:
如果变量有值,则没有问题.
但如果它没有值,则插入中断错误.
我想问:我们如何通过DbCommand参数将可空的DateTime插入Sql?
(P.S.:Sql列也可以为空.)
DateTime? myDate = null; DbCommand dbCommand = new DbCommand(); dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, myDate);试试 null coalescing operator:
dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, (object) myDate ?? DbNull.Value);
查看更多关于c# – 如何将Nullable DateTime变量的null值转换为DbNull.Value的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did69326