【问题标题】:Can I pass a DBNull to Integer type variable?我可以将 DBNull 传递给 Integer 类型变量吗?
【发布时间】:2011-12-07 06:18:19
【问题描述】:

这是否可以将 DB null 发送到整数变量。

我正在调用一个函数

private void BindGridView(String Fromdate, String Todate, int IsPending)
  1. 从日期
  2. 今天
  3. ispending 是我的存储过程标量变量

在页面加载时,我会显示这两个细节(待处理或不待处理)。为此,我需要传递 null。

是否需要更改函数的签名?

【问题讨论】:

  • 为什么 'IsPending' 不是布尔值?
  • @Daniel Casserly Bool 仅支持 False 或 true 不为空

标签: c# asp.net sql-server-2008 stored-procedures


【解决方案1】:

使 int 参数可以为空,然后在调用 sproc 时检查一个值:

private void BindGridView(String Fromdate, String Todate, int? IsPending) {

然后

cmd.Parameters.AddWithValue("@intParam", 
                 IsPending.HasValue ? (object)IsPending.Value : DBNull.Value);

【讨论】:

    【解决方案2】:

    尝试可空类型。在 C# 中,您可以使用问号。而不是 Nullable 你可以写 int ?

    private void BindGridView(String Fromdate, String Todate, int ? IsPending)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2021-10-15
      • 2019-09-24
      • 1970-01-01
      相关资源
      最近更新 更多