我正在使用sqlite
并尝试插入一个DateTime
值,现在在某些情况下该值是null
,并且出现以下警告:
可为空的对象必须具有一个值
导致故障的代码是这样的:
command.Parameters.AddWithValue("Date", item.Start?.DateTime.Value.Date.ToString("yyyy-MM-dd"));
command
是SQLiteCommand
,item
是Google Calendar Event
(我正在使用Google Calendar API)。您如何看待我已尝试修复使用
Start
将?
声明为可为空,但是遇到了同样的问题。Start
的值是这样的:{Google.Apis.Calendar.v3.Data.EventDateTime}
这是
.Start
中包含的属性的图像:Test
我怎样才能解决这个问题?
最佳答案
在这种情况下,对象为C#null,则应使用DB null,即DBNull.Value。
command.Parameters.AddWithValue("Date", (item.Start) ?? DBNull.Value);
关于c# - 无法在sqlite数据库中插入可为空的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490938/