当数据库具有空值时,如何在 ASP.NET Web API 中显示数据为空?
How to show data is null in ASP.NET Web API when database having null values?
我正在从我的 ASP.Net Web API 调用我的数据库作为
[HttpGet]
public IActionResult GetOutput(int id)
{
string CheckJob = "exec sp_GetJob "+ "@id= " + id;
var result = _context.Job.FromSqlRaw(CheckJob).ToList().FirstOrDefault().Job;
if(result == null){
-- do other functions
}
return Ok(result);
}
我的class是
public class Job
{
public int Job {get;set;}
}
并且如果对于特定输入,如果值不可用,那么它应该在我的变量结果中存储空值。但它显示错误为
System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.'
存储过程
CREATE PROCEDURE sp_GetJob @id
AS
DECLARE @JobID AS int
BEGIN
set @JobID = ( select JobId from Config
where id = @id)
SELECT @JobID AS Job
END
有人可以告诉我,如果数据库是 return 空值,那么如何在变量中存储空值,以便我可以比较该变量以继续前进?
谢谢
您可以尝试使用可空值类型。
public class Job
{
public int? Job {get;set;}
}
我正在从我的 ASP.Net Web API 调用我的数据库作为
[HttpGet]
public IActionResult GetOutput(int id)
{
string CheckJob = "exec sp_GetJob "+ "@id= " + id;
var result = _context.Job.FromSqlRaw(CheckJob).ToList().FirstOrDefault().Job;
if(result == null){
-- do other functions
}
return Ok(result);
}
我的class是
public class Job
{
public int Job {get;set;}
}
并且如果对于特定输入,如果值不可用,那么它应该在我的变量结果中存储空值。但它显示错误为
System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.'
存储过程
CREATE PROCEDURE sp_GetJob @id
AS
DECLARE @JobID AS int
BEGIN
set @JobID = ( select JobId from Config
where id = @id)
SELECT @JobID AS Job
END
有人可以告诉我,如果数据库是 return 空值,那么如何在变量中存储空值,以便我可以比较该变量以继续前进?
谢谢
您可以尝试使用可空值类型。
public class Job
{
public int? Job {get;set;}
}