DataColumn.DataType プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataColumn.DataType プロパティの意味・解説 

DataColumn.DataType プロパティ

列に格納されているデータの型を取得または設定します。

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentException

この列には既にデータが格納されています。

または

AutoIncrement が true に設定されていますが、AutoIncrement がサポートしない型に値が設定されています。

解説解説

DataType 値の設定は、データ ソース内のデータを正しく作成および更新できるようにするために重要です。

DataType プロパティは、次に示す基本 .NET Framework データ型をサポートします。

次に示す配列型もサポートされます。

列へのデータの格納が開始された後でこのプロパティを変更すると、例外が生成されます。

DataType プロパティを設定する前に AutoIncrement を true に設定し、整数型以外の型に設定しようとすると、例外が生成されます。

メモメモ

Byte[] データ型の列で、特別な処理を必要とする場合があります。なぜなら、このデータ型は、基本 .NET Framework データ型とは異なり参照データ型だからです。Byte[] データ型の列が PrimaryKey として使用されている場合、または DataView の Sort または 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 に追加し、そのテーブルに 1 行を追加する例を次に示します。

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;  
 }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「DataColumn.DataType プロパティ」の関連用語

DataColumn.DataType プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataColumn.DataType プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS