[英]Checking null value for integer from DataBase
我有一個學生表,它引用一個父表作為FK。 但是學生可以年滿18歲,在這種情況下,他應該對自己負責,並且parent_id
設置為null。 我必須檢查id是否為null,但是:
if (dtreader_resp.Read())
{
if(dtreader_resp.GetInt16("resp_id") != null)
{
resp.Resp_id = dtreader_resp.GetInt16("resp_id");
}
}
始終返回true。 有沒有辦法檢查該字段是否為空?
您可以使用IsDBNull或使用可為空的int:
var data = sqlReader["resp_id"] as int?;
if (data.HasValue)
{
var actualValue = data.Value
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.