DataTableReader.GetDataTypeName(Int32) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a string representing the data type of the specified column.
public:
override System::String ^ GetDataTypeName(int ordinal);
public override string GetDataTypeName (int ordinal);
override this.GetDataTypeName : int -> string
Public Overrides Function GetDataTypeName (ordinal As Integer) As String
- ordinal
- Int32
The zero-based column ordinal.
A string representing the column's data type.
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 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),
reader.GetDataTypeName(i));
}
}
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
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;
}
Private Sub TestGetTypeName()
Dim table As DataTable = GetCustomers()
Using reader As New DataTableReader(table)
For i As Integer = 0 To reader.FieldCount - 1
Console.WriteLine("{0}: {1}", _
reader.GetName(i), reader.GetDataTypeName(i))
Next
End Using
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
Private Function GetCustomers() As DataTable
' Create sample Customers table, in order
' to demonstrate the behavior of the DataTableReader.
Dim table As New DataTable
' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(Integer))
table.Columns.Add("Name", GetType(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
End Function
The Console window displays the following results:
ID: Int32
Name: String
The GetDataTypeName method always returns the type of the underlying DataColumn instead of a provider-specific type.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: