DataColumn.DataType 属性

获取或设置存储在列中的数据的类型。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Property DataType As Type
用法
Dim instance As DataColumn
Dim value As Type

value = instance.DataType

instance.DataType = value
public Type DataType { get; set; }
public:
property Type^ DataType {
    Type^ get ();
    void set (Type^ value);
}
/** @property */
public Type get_DataType ()

/** @property */
public void set_DataType (Type value)
public function get DataType () : Type

public function set DataType (value : Type)

属性值

一个 Type 对象,它表示列数据类型。

异常

异常类型 条件

ArgumentException

列已经存储了数据。

- 或 -

AutoIncrementtrue,但值被设置为 AutoIncrement 不支持的类型。

备注

设置 DataType 值对于确保正确创建和更新数据源中的数据非常重要。

DataType 属性支持下列 .NET Framework 基数据类型:

以及下列数组类型:

  • Byte[]

在列开始存储数据之后若更改此属性,就会产生异常。

如果在设置 DataType 属性之前将 AutoIncrement 设置为 true,并且试图将类型设置为整数类型之外的任何类型,则将生成异常。

提示

由于 Byte[] 数据类型不同于基 .NET Framework 数据类型,而是一种引用数据类型,所以 Byte[] 数据类型 的列在某些情况下需要特殊处理。如果 Byte[] 数据类型的列用作 PrimaryKey,或用作 DataViewSort 键或 RowFilter 键,则对该列值的任何更改都必须包括将 Byte[] 列值赋给单独实例化的 Byte[] 对象。需要通过此赋值操作来触发对排序、筛选和主键操作所使用的内部索引的更新。下面的示例对此进行了阐释:

byte[] columnValue = (byte[])myDataTable.Rows[0][0];
byte[] newValue = (byte[])columnValue.Clone();
newValue[1] = 2;
myDataTable.Rows[0][0] = newValue;

提示

尽管可以将列定义为基 .NET Framework 数据类型和 Byte[] 以外的数据类型,但这样的列将作为用户定义的类型进行处理,具有下列使用限制。(有关用户定义的类型的更多信息,请参见 创建和使用用户定义类型。)

示例

下面的示例将几种数据类型的列添加到 DataTable 中,然后向该表添加一行。

Public Function MakeDataTable() As DataTable
    
    Dim myTable As DataTable 
    Dim myNewRow As DataRow 
    ' Create a new DataTable.
    myTable = New DataTable("My Table")
 
    ' Create DataColumn objects of data types.
    Dim colString As DataColumn = New DataColumn("StringCol")
    colString.DataType = System.Type.GetType("System.String")
    myTable.Columns.Add(colString) 
 
    Dim colInt32 As DataColumn = New DataColumn("Int32Col")
    colInt32.DataType = System.Type.GetType("System.Int32")
    myTable.Columns.Add(colInt32)
 
    Dim colBoolean As DataColumn = New DataColumn("BooleanCol")
    colBoolean.DataType = System.Type.GetType("System.Boolean")
    myTable.Columns.Add(colBoolean)
 
    Dim colTimeSpan As DataColumn = New DataColumn("TimeSpanCol")
    colTimeSpan.DataType = System.Type.GetType("System.TimeSpan")
    myTable.Columns.Add(colTimeSpan)
 
    Dim colDateTime As DataColumn = New DataColumn("DateTimeCol")
    colDateTime.DataType = System.Type.GetType("System.DateTime")
    myTable.Columns.Add(colDateTime)
 
    Dim colDecimal As DataColumn = New DataColumn("DecimalCol")
    colDecimal.DataType = System.Type.GetType("System.Decimal")
    myTable.Columns.Add(colDecimal)
 
    ' Populate one row with values.
    myNewRow = myTable.NewRow()
 
    myNewRow("StringCol") = "Item Name"
    myNewRow("Int32Col") = 2147483647
    myNewRow("BooleanCol") = True
    myNewRow("TimeSpanCol") = New TimeSpan(10,22,10,15,100)
    myNewRow("DateTimeCol") = System.DateTime.Today
    myNewRow("DecimalCol") = 64.0021
    myTable.Rows.Add(myNewRow)
    MakeDataTable = myTable  
 End Function
public DataTable MakeDataTable(){
    
    DataTable myTable;
    DataRow myNewRow; 
    // Create a new DataTable.
    myTable = new DataTable("My Table");
 
    // Create DataColumn objects of data types.
    DataColumn colString = new DataColumn("StringCol");
    colString.DataType = System.Type.GetType("System.String");
    myTable.Columns.Add(colString); 
 
    DataColumn colInt32 = new DataColumn("Int32Col");
    colInt32.DataType = System.Type.GetType("System.Int32");
    myTable.Columns.Add(colInt32);
 
    DataColumn colBoolean = new DataColumn("BooleanCol");
    colBoolean.DataType = System.Type.GetType("System.Boolean");
    myTable.Columns.Add(colBoolean);
 
    DataColumn colTimeSpan = new DataColumn("TimeSpanCol");
    colTimeSpan.DataType = System.Type.GetType("System.TimeSpan");
    myTable.Columns.Add(colTimeSpan);
 
    DataColumn colDateTime = new DataColumn("DateTimeCol");
    colDateTime.DataType = System.Type.GetType("System.DateTime");
    myTable.Columns.Add(colDateTime);
 
    DataColumn colDecimal = new DataColumn("DecimalCol");
    colDecimal.DataType = System.Type.GetType("System.Decimal");
    myTable.Columns.Add(colDecimal);
 
    // Populate one row with values.
    myNewRow = myTable.NewRow();
 
    myNewRow["StringCol"] = "Item Name";
    myNewRow["Int32Col"] = 2147483647;
    myNewRow["BooleanCol"] = true;
    myNewRow["TimeSpanCol"] = new TimeSpan(10,22,10,15,100);
    myNewRow["DateTimeCol"] = System.DateTime.Today;
    myNewRow["DecimalCol"] = 64.0021;
    myTable.Rows.Add(myNewRow);
    return myTable;  
 }

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataColumn 类
DataColumn 成员
System.Data 命名空间
Type
GetType