先来看看其他博主的回答如下

在使用SqlClient的DataReader的时候,如果某一个字段值为NULL并且使用reader.getString方法获取的时候会出现“Data is Null. This method or property cannot be called on Null values.”异常。初步分析导致该异常的原因应该是:

1、NULL值在DataReader中的类型是DBNULL。

2、DBNULL和字符串的空白值String.Empty不是同样的概念。

3、由于DBNULL和String.Empty不是同等值,因此SqlClient默认的实现没有对其进行强制转化而是直接抛出异常。

假装是分割线

查看错误的堆栈跟踪信息,确实报错的方法和上面讲的一样,但是数据表对应字段为null,model对应为string之前也没有出错过,于是抱着试试看的心态把string改成了string?,然后还是报错

最终根据数据表重新生成了mode解决问题,查看git对比,除了几个最大长度的设置差异外就是一些备注差异,于是我把model里一个string最大长度设置成和数据库不一样的,结果未能复现bug。

前后model修改放下面,有知道的可以在下面评论。

旧model

 #region 表字段
        /// <summary>
        /// 停线开始时间
        /// </summary>
        [Display(Name = "停线开始时间")]
        [Column("line_stop_time_start", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? LineStopTimeStart { get; set; }

        /// <summary>
        /// 不良数
        /// </summary>
        [Display(Name = "不良数")]
        [DisplayFormat(DataFormatString = "18,6")]
        [Column("rejection_number", TypeName = "decimal")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public decimal? RejectionNumber { get; set; }

        /// <summary>
        /// 批准人姓名
        /// </summary>
        [Display(Name = "批准人姓名")]
        [MaxLength(64)]
        [Column("ratifier_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        public string RatifierName { get; set; }

        /// <summary>
        /// 记录人名字
        /// </summary>
        [Display(Name = "记录人名字")]
        [MaxLength(32)]
        [Column("recorder_name", TypeName = "nvarchar(32)")]
        [Editable(true)]
        public string RecorderName { get; set; }
        /// <summary>
        /// 开单部门名字
        /// </summary>
        [Display(Name = "开单部门名字")]
        [MaxLength(32)]
        [Column("billing_dept_name", TypeName = "nvarchar(32)")]
        [Editable(true)]
        public string BillingDeptName { get; set; }

        /// <summary>
        /// 异常类别 0=来料,1=制程,2=成品,3=出库  从此往后为金属元素的:10=分析检测反馈,11=设备异常反馈,12=现场异常反馈,13=实效异常反馈
        /// </summary>
        [Display(Name = "异常类别 0=来料,1=制程,2=成品,3=出库  从此往后为金属元素的:10=分析检测反馈,11=设备异常反馈,12=现场异常反馈,13=实效异常反馈 ")]
        [Column("exception_type", TypeName = "int")]
        [Editable(true)]
        public int? ExceptionType { get; set; }

        /// <summary>
        /// 品质工程师id
        /// </summary>
        [Display(Name = "品质工程师id")]
        [Column("quality_engineer_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? QualityEngineerId { get; set; }

        /// <summary>
        /// 开单部门
        /// </summary>
        [Display(Name = "开单部门")]
        [Column("billing_dept_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? BillingDeptId { get; set; }

        /// <summary>
        /// 发现日期
        /// </summary>
        [Display(Name = "发现日期")]
        [Column("discover_datetime", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? DiscoverDatetime { get; set; }

        /// <summary>
        /// 品质工程师姓名
        /// </summary>
        [Display(Name = "品质工程师姓名")]
        [MaxLength(510)]
        [Column("quality_engineer_name", TypeName = "nvarchar(510)")]
        [Editable(true)]
        public string QualityEngineerName { get; set; }

        /// <summary>
        /// 生产数量
        /// </summary>
        [Display(Name = "生产数量")]
        [DisplayFormat(DataFormatString = "18,6")]
        [Column("production_number", TypeName = "decimal")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public decimal? ProductionNumber { get; set; }

        /// <summary>
        /// 异常描述
        /// </summary>
        [Display(Name = "异常描述")]
        [MaxLength(512)]
        [Column("exception_description", TypeName = "nvarchar(512)")]
        [Editable(true)]
        public string ExceptionDescription { get; set; }

        /// <summary>
        /// 批处理号
        /// </summary>
        [Display(Name = "批处理号")]
        [MaxLength(32)]
        [Column("batch_no", TypeName = "varchar(32)")]
        [Editable(true)]
        public string BatchNo { get; set; }

        /// <summary>
        /// 审核人id
        /// </summary>
        [Display(Name = "审核人id")]
        [Column("checker_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? CheckerId { get; set; }

        /// <summary>
        /// 记录人
        /// </summary>
        [Display(Name = "记录人")]
        [Column("recorder_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? RecorderId { get; set; }

        /// <summary>
        /// 审核人姓名
        /// </summary>
        [Display(Name = "审核人姓名")]
        [MaxLength(64)]
        [Column("checker_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        public string CheckerName { get; set; }

        /// <summary>
        /// 批准人id
        /// </summary>
        [Display(Name = "批准人id")]
        [Column("ratifier_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? RatifierId { get; set; }

        /// <summary>
        ///状态0=草稿,1=品质部门审批中,2=工程部门审批中,3=制造部门审批中,4=销售部门审批中,5=品质副总审批中,6=执行中,7=已完结,10=开单部门审核中,11=开单部门批准中  此后为金属元素内容:30=草稿(班长判定中),32=产品负责人判定中,33=班长整改中,34=质量科确认中,35=已处理
        /// </summary>
        [Display(Name = "状态0=草稿,1=品质部门审批中,2=工程部门审批中,3=制造部门审批中,4=销售部门审批中,5=品质副总审批中,6=执行中,7=已完结,10=开单部门审核中,11=开单部门批准中  此后为金属元素内容:30=草稿(班长判定中),32=产品负责人判定中,33=班长整改中,34=质量科确认中,35=已处理")]
        [Column("state", TypeName = "int")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public int State { get; set; }

        /// <summary>
        /// 产品名称
        /// </summary>
        [Display(Name = "产品名称")]
        [MaxLength(64)]
        [Column("product_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public string ProductName { get; set; }

        /// <summary>
        /// 发生类型
        /// </summary>
        [Display(Name = "发生类型")]
        [Column("occurrence_type", TypeName = "int")]
        [Editable(true)]
        public int? OccurrenceType { get; set; }

        /// <summary>
        /// 数据来源:0=产品化,1=金属元素
        /// </summary>
        [Display(Name = "数据来源:0=产品化,1=金属元素")]
        [Column("type", TypeName = "int")]
        [Editable(true)]
        public int? Type { get; set; }

        /// <summary>
        /// 生产订单
        /// </summary>
        [Display(Name = "生产订单")]
        [MaxLength(32)]
        [Column("production_order", TypeName = "varchar(32)")]
        [Editable(true)]
        public string ProductionOrder { get; set; }

        /// <summary>
        /// 不良率
        /// </summary>
        [Display(Name = "不良率")]
        [MaxLength(8)]
        [Column("rejection_rate", TypeName = "varchar(8)")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public string RejectionRate { get; set; }

        /// <summary>
        /// 存放位置
        /// </summary>
        [Display(Name = "存放位置")]
        [MaxLength(32)]
        [Column("storage_location", TypeName = "varchar(32)")]
        [Editable(true)]
        public string StorageLocation { get; set; }

        /// <summary>
        /// 停线结束时间
        /// </summary>
        [Display(Name = "停线结束时间")]
        [Column("line_stop_time_end", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? LineStopTimeEnd { get; set; }

        /// <summary>
        /// 制造时间
        /// </summary>
        [Display(Name = "制造时间")]
        [Column("manufacture_datetime", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? ManufactureDatetime { get; set; }

        /// <summary>
        /// 异常单号
        /// </summary>
        [Display(Name = "异常单号")]
        [MaxLength(32)]
        [Column("sheet_number", TypeName = "varchar(32)")]
        [Editable(true)]
        public string SheetNumber { get; set; }


        /// <summary>
        /// 完成日期
        /// </summary>
        [Display(Name = "完成日期")]
        [Column("finish_datetime", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? FinishDatetime { get; set; }


        /// <summary>
        /// 是否采用反应计划 0否 1是
        /// </summary>
        [Display(Name = "是否采用反应计划 0否 1是")]
        [Column("is_adopt_response_plan", TypeName = "int")]
        [Editable(true)]
        public int? IsAdoptResponsePlan { get; set; }

        /// <summary>
        /// 反应计划
        /// </summary>
        [Display(Name = "反应计划")]
        [MaxLength(256)]
        [Column("adopt_response_plan", TypeName = "nvarchar(256)")]
        [Editable(true)]
        public string AdoptResponsePlan { get; set; }
        #endregion

        #region 显示字段
        #endregion

        #region 关联表
        #endregion

        #region 子表
        /// <summary>
        /// 异常处理详情
        /// </summary>
        [Display(Name = "异常处理详情")]
        public List<BizExceptionHandlingDetail> BizExceptionHandlingDetail { get; set; }

        #endregion

新model

#region 表字段
        /// <summary>
        /// 停线开始时间
        /// </summary>
        [Display(Name = "停线开始时间")]
        [Column("line_stop_time_start", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? LineStopTimeStart { get; set; }

        /// <summary>
        /// 不良数
        /// </summary>
        [Display(Name = "不良数")]
        [DisplayFormat(DataFormatString = "18,6")]
        [Column("rejection_number", TypeName = "decimal")]
        [Editable(true)]
        public decimal? RejectionNumber { get; set; }

        /// <summary>
        /// 批准人姓名
        /// </summary>
        [Display(Name = "批准人姓名")]
        [MaxLength(32)]
        [Column("ratifier_name", TypeName = "nvarchar(32)")]
        [Editable(true)]
        public string RatifierName { get; set; }

        /// <summary>
        /// 异常类别 0=来料,1=制程,2=成品,3=出库  从此往后为金属元素的:10=分析检测反馈,11=设备异常反馈,12=现场异常反馈,13=实效异常反馈
        /// </summary>
        [Display(Name = "异常类别 0=来料,1=制程,2=成品,3=出库  从此往后为金属元素的:10=分析检测反馈,11=设备异常反馈,12=现场异常反馈,13=实效异常反馈")]
        [Column("exception_type", TypeName = "int")]
        [Editable(true)]
        public int? ExceptionType { get; set; }

        /// <summary>
        /// 数据来源:0=产品化,1=金属元素
        /// </summary>
        [Display(Name = "数据来源:0=产品化,1=金属元素")]
        [Column("type", TypeName = "int")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public int Type { get; set; }

        /// <summary>
        /// 品质工程师id
        /// </summary>
        [Display(Name = "品质工程师id")]
        [Column("quality_engineer_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? QualityEngineerId { get; set; }

        /// <summary>
        /// 开单部门
        /// </summary>
        [Display(Name = "开单部门")]
        [Column("billing_dept_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? BillingDeptId { get; set; }

        /// <summary>
        /// 发现日期
        /// </summary>
        [Display(Name = "发现日期")]
        [Column("discover_datetime", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? DiscoverDatetime { get; set; }

        /// <summary>
        /// 品质工程师姓名
        /// </summary>
        [Display(Name = "品质工程师姓名")]
        [MaxLength(510)]
        [Column("quality_engineer_name", TypeName = "nvarchar(510)")]
        [Editable(true)]
        public string QualityEngineerName { get; set; }

        /// <summary>
        /// 生产数量
        /// </summary>
        [Display(Name = "生产数量")]
        [DisplayFormat(DataFormatString = "18,6")]
        [Column("production_number", TypeName = "decimal")]
        [Editable(true)]
        public decimal? ProductionNumber { get; set; }

        /// <summary>
        /// 是否采用反应计划 0否 1是
        /// </summary>
        [Display(Name = "是否采用反应计划 0否 1是")]
        [Column("is_adopt_response_plan", TypeName = "int")]
        [Editable(true)]
        public int? IsAdoptResponsePlan { get; set; }

        /// <summary>
        /// 异常描述
        /// </summary>
        [Display(Name = "异常描述")]
        [MaxLength(512)]
        [Column("exception_description", TypeName = "nvarchar(512)")]
        [Editable(true)]
        public string ExceptionDescription { get; set; }

        /// <summary>
        /// 记录人名字
        /// </summary>
        [Display(Name = "记录人名字")]
        [MaxLength(64)]
        [Column("recorder_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        public string RecorderName { get; set; }

        /// <summary>
        /// 批处理号
        /// </summary>
        [Display(Name = "批处理号")]
        [MaxLength(32)]
        [Column("batch_no", TypeName = "varchar(32)")]
        [Editable(true)]
        public string BatchNo { get; set; }

        /// <summary>
        /// 审核人id
        /// </summary>
        [Display(Name = "审核人id")]
        [Column("checker_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? CheckerId { get; set; }

        /// <summary>
        /// 记录人id
        /// </summary>
        [Display(Name = "记录人id")]
        [Column("recorder_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? RecorderId { get; set; }

        /// <summary>
        /// 开单部门名字
        /// </summary>
        [Display(Name = "开单部门名字")]
        [MaxLength(64)]
        [Column("billing_dept_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        public string BillingDeptName { get; set; }

        /// <summary>
        /// 反应计划
        /// </summary>
        [Display(Name = "反应计划")]
        [MaxLength(512)]
        [Column("adopt_response_plan", TypeName = "nvarchar(512)")]
        [Editable(true)]
        public string AdoptResponsePlan { get; set; }

        /// <summary>
        /// 审核人姓名
        /// </summary>
        [Display(Name = "审核人姓名")]
        [MaxLength(64)]
        [Column("checker_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        public string CheckerName { get; set; }

        /// <summary>
        /// 批准人id
        /// </summary>
        [Display(Name = "批准人id")]
        [Column("ratifier_id", TypeName = "uniqueidentifier")]
        [Editable(true)]
        public Guid? RatifierId { get; set; }

        /// <summary>
        /// 状态0=草稿,1=品质部门审批中,2=工程部门审批中,3=制造部门审批中,4=销售部门审批中,5=品质副总审批中,6=执行中,7=已完结,10=开单部门审核中,11=开单部门批准中  此后为金属元素内
        /// </summary>
        [Display(Name = "状态0=草稿,1=品质部门审批中,2=工程部门审批中,3=制造部门审批中,4=销售部门审批中,5=品质副总审批中,6=执行中,7=已完结,10=开单部门审核中,11=开单部门批准中  此后为金属元素内")]
        [Column("state", TypeName = "int")]
        [Editable(true)]
        [Required(AllowEmptyStrings = false)]
        public int State { get; set; }

        /// <summary>
        /// 产品名称
        /// </summary>
        [Display(Name = "产品名称")]
        [MaxLength(64)]
        [Column("product_name", TypeName = "nvarchar(64)")]
        [Editable(true)]
        public string ProductName { get; set; }

        /// <summary>
        /// 发生类型(0=首次,1=重复)
        /// </summary>
        [Display(Name = "发生类型(0=首次,1=重复)")]
        [Column("occurrence_type", TypeName = "int")]
        [Editable(true)]
        public int? OccurrenceType { get; set; }

        /// <summary>
        /// 生产订单
        /// </summary>
        [Display(Name = "生产订单")]
        [MaxLength(32)]
        [Column("production_order", TypeName = "varchar(32)")]
        [Editable(true)]
        public string ProductionOrder { get; set; }

        /// <summary>
        /// 不良率
        /// </summary>
        [Display(Name = "不良率")]
        [MaxLength(8)]
        [Column("rejection_rate", TypeName = "varchar(8)")]
        [Editable(true)]
        public string RejectionRate { get; set; }

        /// <summary>
        /// 存放位置
        /// </summary>
        [Display(Name = "存放位置")]
        [MaxLength(32)]
        [Column("storage_location", TypeName = "varchar(32)")]
        [Editable(true)]
        public string StorageLocation { get; set; }

        /// <summary>
        /// 完成日期
        /// </summary>
        [Display(Name = "完成日期")]
        [Column("finish_datetime", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? FinishDatetime { get; set; }

        /// <summary>
        /// 停线结束时间
        /// </summary>
        [Display(Name = "停线结束时间")]
        [Column("line_stop_time_end", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? LineStopTimeEnd { get; set; }

        /// <summary>
        /// 制造时间
        /// </summary>
        [Display(Name = "制造时间")]
        [Column("manufacture_datetime", TypeName = "datetime")]
        [Editable(true)]
        public DateTime? ManufactureDatetime { get; set; }

        /// <summary>
        /// 异常单号
        /// </summary>
        [Display(Name = "异常单号")]
        [MaxLength(32)]
        [Column("sheet_number", TypeName = "varchar(32)")]
        [Editable(true)]
        public string SheetNumber { get; set; }

        #endregion
        
        #region 显示字段
        #endregion
        
        #region 关联表
        #endregion
        
        #region 子表
        /// <summary>
        /// 异常处理详情
        /// </summary>
        [Display(Name = "异常处理详情")]
        public List<BizExceptionHandlingDetail> BizExceptionHandlingDetail { get; set; }

        #endregion
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐