c# - 无法在sqlite数据库中插入可为空的值

标签 c# sqlite

我正在使用sqlite并尝试插入一个DateTime值,现在在某些情况下该值是null,并且出现以下警告:


可为空的对象必须具有一个值


导致故障的代码是这样的:

command.Parameters.AddWithValue("Date", item.Start?.DateTime.Value.Date.ToString("yyyy-MM-dd"));


commandSQLiteCommanditemGoogle 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/

相关文章:

c# - 在服务器中启动新进程是否存在安全漏洞?

android - 错误: Class is referenced as a converter but it does not have any converter methods

android - sqlite查询错误,android

Android Sqlite 查询检测表中何时不存在元素

C# 创建像 PHP 一样的可调用对象

c# - 是否将用户设置存储在 ASP.NET Core Identity AspNetUsers 表中

c# - 如何从字符串中删除换行符?

c# - 将图像从 OpenCV/C++ 传递回 .NET C#

c++ - 在发布文件夹中运行的 Qt 可执行文件 - Program Files (x86) 中的运行时错误

java - 为类的任何函数编写可线程包装器?