关于DBNull.Value

简介:

I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so:

DbParameter datePrm = updateStmt.CreateParameter();
datePrm
.ParameterName = "@change_date";

And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for nulls.

I thought initially I'd be clever:

datePrm.Value = nullableDate ?? DBNull.Value;

but that fails with the error

Operator '??' cannot be applied to operands of type 'System.DateTime?' and 'System.DBNull'

So I guess that only works if the second argument is a non-nullable version of the first argument. So then I went for:

datePrm.Value = nullableDate.HasValue ? nullableDate.Value : DBNull.Value;

but that doesn't work either:

Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'System.DBNull'

But I don't want to convert between those types!

So far the only thing I can get to work is:

if (nullableDate.HasValue)
  datePrm
.Value = nullableDate.Value;
else
  datePrm
.Value = DBNull.Value;

Is that really the only way I can write this? Is there a way to get a one-liner using the ternary operator to work?

Update: I don't really get why the ?? version doesn't work. MSDN says:

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

That's exactly what I want!

Update2: Well it was kind of obvious in the end:

datePrm.Value = nullableDate ?? (object)DBNull.Value;









本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/415825,如需转载请自行联系原作者
目录
相关文章
|
数据可视化 安全 网络安全
Window下安装并使用InfluxDB可视化工具 —— InfluxDBStudio
Window下安装并使用InfluxDB可视化工具 —— InfluxDBStudio
4916 0
Window下安装并使用InfluxDB可视化工具 —— InfluxDBStudio
|
小程序 开发工具 Android开发
uni-app使用HBuilder X编辑器本地打包apk步骤说明
uni-app使用HBuilder X编辑器本地打包apk步骤说明
4528 0
|
5月前
|
人工智能 边缘计算 C#
纯C#驱动全场景视觉AI:.NET 10+YOLO多模型平台赋能工业与边缘智能
基于.NET 10与YOLO技术的纯C#视觉AI平台,支持多模型并行、跨平台部署,集成目标检测、分割、姿态估计等全任务,无需Python依赖,助力工业质检、智能安防、零售分析等场景高效落地。
|
分布式计算 大数据 Apache
利用.NET进行大数据处理:Apache Spark与.NET for Apache Spark
【10月更文挑战第15天】随着大数据成为企业决策和技术创新的关键驱动力,Apache Spark作为高效的大数据处理引擎,广受青睐。然而,.NET开发者面临使用Spark的门槛。本文介绍.NET for Apache Spark,展示如何通过C#和F#等.NET语言,结合Spark的强大功能进行大数据处理,简化开发流程并提升效率。示例代码演示了读取CSV文件及统计分析的基本操作,突显了.NET for Apache Spark的易用性和强大功能。
519 1
QGS
|
SQL 弹性计算 Java
手搭手入门Spring boot+Mybatis+达梦数据库(国产数据库)
手搭手入门Spring boot+Mybatis+达梦数据库(国产数据库)
QGS
1874 0
|
SQL 开发框架 前端开发
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
|
API Docker Windows
2024 Ollama 一站式解决在Windows系统安装、使用、定制服务与实战案例
这篇文章是一份关于Ollama工具的一站式使用指南,涵盖了在Windows系统上安装、使用和定制服务,以及实战案例。
2024 Ollama 一站式解决在Windows系统安装、使用、定制服务与实战案例
|
存储 资源调度 前端开发
JavaScript 使用axios库发送 post请求给后端, 给定base64格式的字符串数据和一些其他参数, 使用表单方式提交, 并使用onUploadProgress显示进度
使用 Axios 发送包含 Base64 数据和其他参数的 POST 请求时,可以通过 `onUploadProgress` 监听上传进度。由于整个请求体被视为一个单元,所以进度可能不够精确,但可以模拟进度反馈。前端示例代码展示如何创建一个包含 Base64 图片数据和额外参数的 `FormData` 对象,并在上传时更新进度条。后端使用如 Express 和 Multer 可处理 Base64 数据。注意,实际进度可能不如文件上传精确,显示简单加载状态可能更合适。
|
安全 Java 数据库连接
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
4685 6
|
存储 算法 NoSQL
C# 实现分布式自增 ID 算法(Snowflake 雪花算法)
需求概述分布式系统中,有一些需要使用 `全局唯一 ID` 的场景,这种时候为了防止 `ID` 冲突可以使用 `36` 位的通用唯一识别码/UUID(Universally Unique Identifier),但是 `UUID` 有一些缺点,首先他相对比较长,另外 `UUID` 一般是无序的。有些时候我们希望能使用一种简单一些的 ID,并且希望 ID 能够按照时...
1545 1
C# 实现分布式自增 ID 算法(Snowflake 雪花算法)