操作'?“不能适用于类型“串”和“System.DBNull'的操作数 [英] Operator '??' cannot be applied to operands of type 'string' and 'System.DBNull'

查看:388
本文介绍了操作'?“不能适用于类型“串”和“System.DBNull'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的C#code:

I have the following C# code:

sqlCommand.Parameters.AddWithValue("@Parameter", table.Value ?? DBNull.Value);

但它引发以下编译错误:

But it throws the following compilation error:

操作 ?? 不能应用于类型的操作数字符串 System.DBNull

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

为什么没有编译器允许这种语法?

Why doesn't the compiler allow this syntax?

推荐答案

两个操作数需对象。使用显式类型转换:

Both operands need to be object. Use explicit cast:

(object)table.Value ?? DBNull.Value;

这篇关于操作'?“不能适用于类型“串”和“System.DBNull'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆