简体   繁体   English

从数据库检查整数的空值

[英]Checking null value for integer from DataBase

I have a student table that references a parent table, as a FK. 我有一个学生表,它引用一个父表作为FK。 But a student can be older than 18, in which case he's responsable for himself and the parent_id is set to null. 但是学生可以年满18岁,在这种情况下,他应该对自己负责,并且parent_id设置为null。 I have to check if the id is null, but: 我必须检查id是否为null,但是:

if (dtreader_resp.Read())
            {
                if(dtreader_resp.GetInt16("resp_id") != null)
                {
                    resp.Resp_id = dtreader_resp.GetInt16("resp_id");
                }
            }

Always returns true. 始终返回true。 Is there a way to check if that field is null? 有没有办法检查该字段是否为空?

You could use IsDBNull or use a nullable int: 您可以使用IsDBNull或使用可为空的int:

var data = sqlReader["resp_id"] as int?;
if (data.HasValue)
{
    var actualValue = data.Value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM