简体   繁体   English

将Null值插入整数列

[英]Insert Null value to the Integer Column

Through the front end I want to insert NULL value to the Column whose DataType is Int. 通过前端,我想将NULL值插入DataType为Int的Column。

I used like this: 我用过这样的:

POP.JobOrderID = Convert.ToInt32(DBNull.Value);

But I cannot Insert Null value, it throws error such as "Object cannot be cast from DBNull to other types": 但是我不能插入Null值,它会抛出错误,例如“对象无法从DBNull转换为其他类型”:

How to insert NULL values? 如何插入NULL值?

if you wish to do it POP.JobOrderID should be type int? 如果你想这样做POP.JobOrderID应该是int?类型int? ( nullable int) not int nullable int)不是int

kleinohad is correct. kleinohad是对的。 Furthermore, you should assign null , not DBNull.Value 此外,您应该指定null ,而不是DBNull.Value

Use Nullable<Int32> or just an alias: int? 使用Nullable<Int32>或只是一个别名: int? .

POP.JobOrderID = 5;
// or
POP.JobOrderID = null;

Usage in ADO.NET: 在ADO.NET中的用法:

command.Parameters.AddWithValue("@JobOrderId", POP.JobOrderID ?? DBNull.Value);

which is equals to: 这相当于:

POP.JobOrderID.HasValue ? POP.JobOrderID.Value : DBNull.Value;

This 这个

   POP.JobOrderID = new Nullable<Int32>();

should work too IF the JobOrderID is a nullable type ( int? ). 如果JobOrderID是可以为空的类型( int? ),也应该工作。

Cheers 干杯

Like this: 像这样:

 POP.JobOrderID = null;    // JobOrderID should be of type int? which is a nullable-int

Hope that helps. 希望有所帮助。

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

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