2011-04-28 99 views
0

我想添加新的列到我的數據表。該列是char類型,長度爲10.設置char datolumn的大小

DataColumn d = new DataColumn(fieldname,typeof(char)); dt.Columns.Add(d);

我的問題是如何將「大小」添加到此列?

非常感謝。

回答

1

請注意,在C#中,一個char總是一個字符。試試這個:

DataColumn d = new DataColumn(fieldname, typeof(string)); 
d.MaxLength = 10; 
dt.Columns.Add(d); 
+0

謝謝。 MaxLength僅適用於字符串數據類型。我正在嘗試char。 – 2011-04-29 13:12:03

+0

@Murali:這是正確的,MaxLength只適用於字符串數據類型。請注意,C#,'char'類型與數據庫'char'列類型不同。在C#中,'char'總是隻有一個字符*。如果你想在C#中支持多個字符,你必須使用'string'。 – kbrimington 2011-05-02 13:22:53