DataTableReader.GetDataTypeName(Int32) Metodo

Definizione

Ottiene una stringa che rappresenta il tipo di dati della colonna specificata.

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

Parametri

ordinal
Int32

Ordinale di colonna in base zero.

Valori restituiti

Stringa che rappresenta il tipo di dati della colonna.

Eccezioni

L'indice passato non è compreso nell'intervallo compreso tra 0 e FieldCount - 1.

È stato effettuato un tentativo di lettura o accesso a una colonna in un oggetto chiuso DataTableReader.

Esempio

L'applicazione console seguente visualizza un elenco di campi e i relativi nomi di tipo da un semplice 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

Nella finestra Console vengono visualizzati i risultati seguenti:

ID: Int32
Name: String

Commenti

Il GetDataTypeName metodo restituisce sempre il tipo dell'oggetto sottostante DataColumn anziché un tipo specifico del provider.

Si applica a