GetDataTypeName Method

DataTableReader.GetDataTypeName Method (Int32)


Gets a string representing the data type of the specified column.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

public override string GetDataTypeName(
	int ordinal


Type: System.Int32

The zero-based column ordinal.

Return Value

Type: System.String

A string representing the column's data type.

Exception Condition

The index passed was outside the range of 0 to FieldCount - 1.


An attempt was made to read or access a column in a closed DataTableReader.

The GetDataTypeName method always returns the type of the underlying DataColumn instead of a provider-specific type.

The following console application displays a list of fields and their type names from a simple DataTable:

private static void TestGetTypeName()
    DataTable table = GetCustomers();
    using (DataTableReader reader = new DataTableReader(table))
        for (int i = 0; i < reader.FieldCount; i++)
            Console.WriteLine("{0}: {1}", reader.GetName(i), 
    Console.WriteLine("Press Enter to finish.");

private static DataTable GetCustomers()
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();
    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string ));
    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    return table;

The Console window displays the following results:

ID: Int32
Name: String

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft