2016-12-03 165 views
-1

將列定義爲可爲空的,我需要在C#VS2013以限定System.Data.DataTable。在一列中在System.Data.DataTable在C#VS2013

,它可能是int或空。

但我得到:

DataSet does not support System.Nullable<>. 

對於定義:

public DataTable myDataTable 
    myDataTable = new DataTable("myName"); 
    myDataTable.Columns.Add("ID", typeof(Int32)); 
    myDataTable.Columns.Add("value1", typeof(Int32?)); // run time error 
    myDataTable.Columns.Add("value2", typeof(Int32?)); 

任何想法?解決?

使得柱Dullable後,

DataColumn dc = myDataTable.Columns.Add("value1", typeof(Int32)); 
dc.AllowDBNull = true; 

當我問它,我得到了

"Sequence contains no elements". 

請參閱UPDATE。

UPDATE

int? result = (from r in myDataTable.AsEnumerable() 
        where r.Field<Int32>("ID") == givenID 
        && r.Field<Int32?>("value1") == givenValue1 
        select r.Field<Int32>("value2")).First(); 

回答

2

這是DataColumn的

public DataTable myDataTable 
myDataTable = new DataTable("myName"); 
myDataTable.Columns.Add("ID", typeof(Int32)); 
DataColumn dc = myDataTable.Columns.Add("value1", typeof(Int32)); 
dc.AllowDBNull = true; 

MSDN about AllowDBNull

+0

謝謝你的屬性,它的工作原理,但是當我問它,我得到了「序列不包含任何元素」。請參閱UPDATE。 – Lily

+0

這是一個不同的問題。如果查詢的結果沒有匹配,那麼你不能首先因爲正是'序列不包含任何元素的使用。而是使用FirstOrDefault – Steve