我有一个可为空的日期时间变量。我想将它写入 SQL DB。当我尝试插入时:
如果变量有值就没有问题。
但如果它没有值,插入就会因错误而中断。
我想问:如何通过DbCommand参数向Sql插入可为空的DateTime?
(P.S.:Sql 列也可以为空。)
DateTime? myDate = null;
DbCommand dbCommand = new DbCommand();
dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, myDate);
Try the 空合并运算符 http://msdn.microsoft.com/en-us/library/ms173224.aspx:
dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, (object) myDate ?? DbNull.Value);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)