Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
param7[1].Value = tbLastName.Text.Length > 0 ? tbLastName.Text : DBNull.Value;
为什么这是不可能的,还有什么建议可以尝试?20 个参数的 if/else 不是办法。
问题是(如错误消息所示)条件表达式需要两个分支上的类型相同,或者需要从一种类型到另一种类型的隐式转换。在你的情况下,没有一个,所以你得到了错误。一种快速修复方法是将其中一个值转换为(object)(这很好,因为无论如何DbParameter.Value都是类型。)object
(object)
DbParameter.Value
object
所以这应该适合你:
param7[1].Value = tbLastName.Text.Length > 0 ? tbLastName.Text : (object)DBNull.Value;