【问题标题】:How to set the property to true that will allow me to insert null values? [duplicate]如何将属性设置为 true 以允许我插入空值? [复制]
【发布时间】:2014-12-11 07:28:38
【问题描述】:

我在“AddEffectiveQuarterTest”方法的单元测试中遇到错误。如何将属性设置为 true 以允许我插入空值?

AvailabilityBLTest.cs 文件:“AddEffectiveQuarterTest”方法

    public void AddEffectiveQuarterTest()
    {
        AvailabilityBL target = new AvailabilityBL();
        AvailabilityDS ds = new AvailabilityDS();
        TestBusinessLogic.BusinessLogic_AvailabilityBLAccessor accessor = new TestBusinessLogic.BusinessLogic_AvailabilityBLAccessor(target);

        // SETUP STANDARD TEST DATA
        this.SetupTestData(ds, null);
        int newModelID = -1;    
        .........
    }

可用性DS.Designer.cs 文件

public string QuarterDisplay {
    get {
            try {
                return ((string)(this[this.tableTime.QuarterDisplayColumn]));
            }
            catch (global::System.InvalidCastException e) {
                throw new global::System.Data.StrongTypingException("The value for column \'QuarterDisplay\' in table \'Time\' is DBNull.", e);
        }
    }
    set {
            this[this.tableTime.QuarterDisplayColumn] = value;
    }
}

如何使用以下代码进行编辑以将属性设置为 true 以允许我插入空值?

public bool AllowDBNull { get; set; }

如何检查 dbnull?我收到以下代码的错误。

 public bool IsColumnNameNull() {
                    return this.IsNull(this.tableColumn.ColumnNameColumn);
                }

 public string SetColumnKeyNull() {
            {
                get
                {
                    try
                    {
                        return ((string)(this[this.tableColumn.ColumnKeyColumn]));
                    }
                    catch (global::System.InvalidCastException e)
                    {
                        throw new global::System.Data.StrongTypingException("The value for column \'SetColumnKeyNull\' in table \'Column\' is DBNull.", e);
                    }
                }
                set
                {
                    this[this.tableColumn.ColumnKeyColumn] = value;
                }
            }

【问题讨论】:

标签: c# unit-testing dbnull


【解决方案1】:

您似乎正在使用强类型数据集。

在这种情况下,生成的代码将包含将字段设置为 null 的方法。

例如,如果您的字段QuarterDisplay 允许DBNull,则AvailabilityDS.Designer.cs 中生成的代码将包含方法SetQuarterDisplayNull()。它还将包含一个方法IsQuarterDisplayNull(),您可以使用它来检查DBNull 值。

【讨论】:

  • 我没有 SetQuarterDisplayNull() 的方法,我应该为这些情况创建另一种方法吗?我只是单元测试的新手......
  • 我没有生成包含方法SetQuarterDisplayNull()和IsQuarterDisplayNull()的代码,还有其他方法可以解决吗?如何修复“空”?使其非空,或执行空测试?
  • 如果没有这些方法,则表示对应的列不可为空。您可以考虑在 Visual Studio DataSet 设计器中打开 AvailabilityDS.xsd 文件,并在列 Properties 中将列的 AllowDBNull 属性更改为 True。这将导致方法被添加到生成的代码中。
  • 解释得很清楚,请问如何检查DBNull值?我在这里修改了一些,forums.asp.net/t/1383849.aspx?How+to+check+dbnull+in+C+,但无法正常工作。
  • "请问如何检查 DBNull 值" - 在强类型数据集中,您可以使用生成的方法IsColumnNameNull(),如答案中所述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
  • 2020-01-17
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 2011-04-22
相关资源
最近更新 更多