DataColumn.DataType 属性

定义

获取或设置存储在列中的数据的类型。Gets or sets the type of data stored in the column.

public:
 property Type ^ DataType { Type ^ get(); void set(Type ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))]
public Type DataType { get; set; }
public Type DataType { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))]
[System.Data.DataSysDescription("DataColumnDataTypeDescr")]
public Type DataType { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))>]
member this.DataType : Type with get, set
member this.DataType : Type with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))>]
[<System.Data.DataSysDescription("DataColumnDataTypeDescr")>]
member this.DataType : Type with get, set
Public Property DataType As Type

属性值

Type

一个 Type 对象,它表示列数据类型。A Type object that represents the column data type.

属性

例外

列已经存储了数据。The column already has data stored.

示例

下面的示例向添加几个数据类型的列 DataTable ,然后将一行添加到表中。The following example adds columns of several data types to a DataTable, and then adds one row to the table.

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);

    DataColumn colByteArray = new DataColumn("ByteArrayCol");
    colByteArray.DataType = System.Type.GetType("System.Byte[]");
    myTable.Columns.Add(colByteArray);

    // 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;
    myNewRow["ByteArrayCol"] = new Byte[] { 1, 5, 120 };
    myTable.Rows.Add(myNewRow);
    return myTable;
 }
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 New DataColumn("StringCol")
    colString.DataType = System.Type.GetType("System.String")
    myTable.Columns.Add(colString) 
 
    Dim colInt32 As New DataColumn("Int32Col")
    colInt32.DataType = System.Type.GetType("System.Int32")
    myTable.Columns.Add(colInt32)
 
    Dim colBoolean As New DataColumn("BooleanCol")
    colBoolean.DataType = System.Type.GetType("System.Boolean")
    myTable.Columns.Add(colBoolean)
 
    Dim colTimeSpan As New DataColumn("TimeSpanCol")
    colTimeSpan.DataType = System.Type.GetType("System.TimeSpan")
    myTable.Columns.Add(colTimeSpan)
 
    Dim colDateTime As New DataColumn("DateTimeCol")
    colDateTime.DataType = System.Type.GetType("System.DateTime")
    myTable.Columns.Add(colDateTime)
 
    Dim colDecimal As 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
    myNewRow("ByteArrayCol") = New [Byte]() {1, 5, 120}
    myTable.Rows.Add(myNewRow)
    MakeDataTable = myTable  
 End Function

注解

设置 DataType 该值对于保证正确创建和更新数据源中的数据非常重要。Setting the DataType value is very important to guaranteeing the correct creation and updating of data in a data source.

DataType属性支持以下基于 .NET Framework 的数据类型:The DataType property supports the following base .NET Framework data types:

以及以下数组类型:as well as the following array type:

  • Byte[]

在列开始存储数据后更改此属性时,将生成异常。An exception is generated when changing this property after the column has begun storing data.

如果在 AutoIncrement true 设置属性之前将设置为 DataType ,并且尝试将类型设置为除整数类型之外的任何内容,则会生成异常。If AutoIncrement is set to true before setting the DataType property, and you try to set the type to anything except an integer type, an exception is generated.

备注

Byte[]在某些情况下,数据类型为的列需要特殊处理,因为与基本 .NET Framework 数据类型不同,它是引用数据类型。A column of data type Byte[] requires special treatment in certain cases since, unlike the base .NET Framework data types, it is a reference data type. 如果将数据类型的列用作 Byte[] PrimaryKey ,或作为 Sort RowFilter 的或键 DataView ,则对列值所做的任何更改都必须涉及将 Byte[] 列值分配给单独的实例化 Byte[] 对象。If a column of data type Byte[] is used as a PrimaryKey, or as a Sort or RowFilter key for a DataView, any change to the column value must involve assigning the Byte[] column value to a separately instantiated Byte[] object. 需要执行此分配以触发对排序、筛选和主键操作所使用的内部索引的更新。This assignment is required to trigger the update of the internal indexes used by sorting, filtering, and primary key operations. 下面的示例对此进行演示:This is illustrated by the following example:

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

备注

尽管可以将列定义为数据类型(而不是基本 .NET Framework 数据类型),但是 Byte[] ,此类列将被视为用户定义类型,但要服从以下使用限制。Although it is possible to define a column as data type other than the base .NET Framework data types and Byte[], such a column will be treated as a user-defined type, subject to the following usage restrictions. (有关用户定义类型的详细信息,请参阅 创建和使用 User-Defined 类型) (For more information on user-defined types, see Creating and Using User-Defined Types)

  • 列不能是 RowFilter or 表达式的一部分 SelectThe column cannot be part of a RowFilter or Select expression.

  • 如果该列用作 PrimaryKey Sort 或作为的或 DataView ,则它必须被视为不可变字段; 一旦将该列添加到表中,就不能更改列数据。If the column is used as a PrimaryKey, or as a Sort or for a DataView, it must be treated as an immutable field; the column data must not be changed once it has been added to the table.

  • 它只能 ColumnMapping 设置为 MappingType.ElementIts ColumnMapping can be only set to MappingType.Element.

  • 实现列的数据类型的类必须用进行标记 SerializableAttribute ,如果需要,还必须实现 ISerializableIXmlSerializable 接口。The class that implements the column's data type must be must be marked with the SerializableAttribute, and if necessary implement the ISerializable or IXmlSerializable interface.

  • 对更改跟踪的支持是有限的。Support for change tracking is limited. 若要利用 DataTable 类的更改跟踪机制,实现列的数据类型的类必须实现 IChangeTracking 接口,或接受通过 DataRow SetModified 在行上调用或通过将列值对象分配给单独实例化的列值对象来对列值进行通知的责任。To utilize the DataTable class' change tracking mechanism, the class that implements the column's data type must either implement the IChangeTracking interface, or take over the responsibility for informing the DataRow when the column value has been modified, either by calling SetModified on the row or by assigning the column value object to a separately instantiated column value object.

适用于

另请参阅