cmd.Parameters.AddWithValue nullable int

后端 未结 3 1618
后悔当初
后悔当初 2021-01-25 08:07

In my data layer class, I initialize a parameter like so:

private int? _DependencyID;

public int? DependencyID
{ get {return _DependencyID;} set {_DependencyID          


        
3条回答
  •  执笔经年
    2021-01-25 08:54

    You need to add an object:

    cmd.AddWithValue("@DependencyID", _DependencyID == null? DBNull.Value : (object)_DependencyID); 
    

    You can shorten that to

    cmd.AddWithValue("@DependencyID", (object)_DependencyID ?? DBNull.Value); 
    

提交回复
热议问题