通过前端,我想将NULL值插入DataType为Int的列。将空值插入整数列
我用这样的:
POP.JobOrderID = Convert.ToInt32(DBNull.Value);
但我不能插入NULL值,它会抛出错误,如“对象不能从DBNull的被转换为其他类型”:
如何插入NULL值?
通过前端,我想将NULL值插入DataType为Int的列。将空值插入整数列
我用这样的:
POP.JobOrderID = Convert.ToInt32(DBNull.Value);
但我不能插入NULL值,它会抛出错误,如“对象不能从DBNull的被转换为其他类型”:
如何插入NULL值?
,如果你想做到这一点POP.JobOrderID
应该是类型int?
(nullable INT)不是int
kleinohad是正确的。此外,应先分配null
,不DBNull.Value
我正在使用三层体系结构。我会像上面那样传递我在问题中给出的参数。如何在这里应用int?(可为空)? – thevan
像这样:
POP.JobOrderID = null; // JobOrderID should be of type int? which is a nullable-int
希望有所帮助。
这
POP.JobOrderID = new Nullable<Int32>();
应太中频JobOrderID
是可空类型(int?
)。
干杯
使用Nullable<Int32>
或只是一个别名:int?
。
POP.JobOrderID = 5;
// or
POP.JobOrderID = null;
用法在ADO.NET:
command.Parameters.AddWithValue("@JobOrderId", POP.JobOrderID ?? DBNull.Value);
其等于:
POP.JobOrderID.HasValue ? POP.JobOrderID.Value : DBNull.Value;
POP是一个对象和JobOrderID是一个Int。如何分配像这样? – thevan
我正在使用三层体系结构。我会像上面那样传递我在问题中给出的参数。如何在这里应用int?(可为空)? – thevan