DataTableReader.GetSchemaTable 方法

定義

傳回 DataTable,以描述 DataTableReader 的資料行中繼資料。

public:
 override System::Data::DataTable ^ GetSchemaTable();
public override System.Data.DataTable GetSchemaTable ();
override this.GetSchemaTable : unit -> System.Data.DataTable
Public Overrides Function GetSchemaTable () As DataTable

傳回

描述資料行中繼資料的 DataTable

例外狀況

範例

下列主控台應用程式範例會擷取指定資料行的架構資訊。 將 DisplaySchemaTableInfoDataTableReader 程式和整數傳遞,代表 資料行在 內的 DataTableReader 序數位置,而程式會將架構資訊輸出至主控台視窗。

private static void TestGetSchemaTable()
{
    // Set up the data adapter, using information from
    // the AdventureWorks sample database.
    // Modify the SQL expression to retrieve
    // data from a different table.
    SqlDataAdapter adapter =
        SetupDataAdapter("SELECT * FROM Sales.Customer");

    // Fill the DataTable, retrieving all the schema information.
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    DataTable table = new DataTable();
    adapter.Fill(table);

    // Create the DataTableReader, and close it when done.
    using (DataTableReader reader = new DataTableReader(table))
    {
        // Modify the column number to display information
        // about a column other than column 0.
        DisplaySchemaTableInfo(reader, 0);
    }

    Console.WriteLine();
    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();
}

private static void DisplaySchemaTableInfo(
        DataTableReader reader, int ordinal)
{
    // Given a DataTableReader, display schema
    // information about a particular column.
    try
    {
        DataTable schemaTable = reader.GetSchemaTable();
        DataRow row = schemaTable.Rows[ordinal];
        foreach (DataColumn col in schemaTable.Columns)
        {
            Console.WriteLine("{0}: {1}",
                col.ColumnName, row[col.Ordinal]);
        }
    }
    catch (IndexOutOfRangeException ex)
    {
        Console.WriteLine("{0} is an invalid column number.",
            ordinal);
    }
}

private static SqlDataAdapter SetupDataAdapter(String sqlString)
{
    // Assuming all the default settings, create a
    // SqlDataAdapter working with the AdventureWorks
    // sample database that's available with
    // SQL Server.
    String connectionString =
        "Data source=(local);initial catalog=AdventureWorks;" +
        "Integrated Security=True";
    return new SqlDataAdapter(sqlString, connectionString);
}
Private Sub TestGetSchemaTable()
   ' Set up the data adapter, using information from 
   ' the AdventureWorks sample database.
   ' Modify the SQL expression to retrieve 
   ' data from a different table.
   Dim adapter As SqlDataAdapter = _
      SetupDataAdapter("SELECT * FROM Sales.Customer")

   ' Fill the DataTable, retrieving all the schema information.
   adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
   Dim table As New DataTable
   adapter.Fill(table)

   ' Create the DataTableReader, and close it when done.
   Using reader As New DataTableReader(table)
      ' Modify the column number to display information
      ' about a column other than column 0.
      DisplaySchemaTableInfo(reader, 0)
   End Using

   Console.WriteLine()
   Console.WriteLine("Press Enter to finish.")
   Console.ReadLine()
End Sub

Private Sub DisplaySchemaTableInfo( _
   ByVal reader As DataTableReader, ByVal ordinal As Integer)

   ' Given a DataTableReader, display schema
   ' information about a particular column.
   Try
      Dim schemaTable As DataTable = reader.GetSchemaTable()
      Dim row As DataRow = schemaTable.Rows(ordinal)
      For Each col As DataColumn In schemaTable.Columns
         Console.WriteLine("{0}: {1}", _
            col.ColumnName, row(col.Ordinal))
      Next
   Catch ex As IndexOutOfRangeException
      Console.WriteLine("{0} is an invalid column number.", _
         ordinal)
   End Try
End Sub

Private Function SetupDataAdapter( _
   ByVal sqlString As String) As SqlDataAdapter
   ' Assuming all the default settings, create a SqlDataAdapter
   ' working with the AdventureWorks sample database that's 
   ' available with SQL Server.
   Dim connectionString As String = _
      "Data Source=(local);" & _
      "Initial Catalog=AdventureWorks;" & _
      "Integrated Security=true"
   Return New SqlDataAdapter(sqlString, connectionString)
End Function

備註

GetSchemaTable 方法會依下列順序傳回每個資料行的相關中繼資料:

DataReader 資料行 描述
ColumnName 資料行出現在 中 DataTable 時的名稱。
ColumnOrdinal 資料行的序數
ColumnSize 如果 ColumnSize 無法判斷或不相關的 (或 MaxLength) 屬性 DataColumn ,則為 -1;否則為 0 或包含 MaxLength 值的正整數。
NumericPrecision 如果資料行類型是數數值型別,則這是資料行的最大精確度。 如果資料行類型不是數值資料類型,則這是 Null 值。
NumericScale 如果資料行資料類型具有小數位陣列件,請將小數點右邊的數位數傳回。 否則,傳回 Null 值。
DataType 資料行的基礎型別。
ProviderType 資料行之資料類型的指標。 如果資料行的資料類型因數據列而異,則此值為 Object 。 這個資料行不能包含 null 值。
IsLong true 如果資料行的資料類型為 String ,且其屬性為 -1,則 MaxLength 為 。 否則為 false
AllowDBNull true 如果資料行的 AllowDbNull 條件約束設定為 true,則為 ;否則為 false
IsReadOnly true 如果無法修改資料行,則為 ;否則為 false
IsRowVersion false,用於每個資料行。
IsUnique true:此 DataTable 資料行中沒有兩個數據列可以有相同的值。 IsUnique 如果資料行本身代表索引鍵,或有唯一類型 UNIQUE 的條件約束僅適用于此資料行,則保證為 true。 false:資料行可以包含 中的 DataTable 重複值。 這個資料行的預設值為 false
IsKey true:資料行是一組資料行之一,會一起唯一識別 中的資料 DataTable 列。 設定 IsKeytrue 的資料行集合必須唯一識別 中的資料 DataTable 列。 不需要這組資料行集是最基本的資料行集。 這組資料行可以從主鍵、唯一 DataTable 條件約束或唯一索引產生。 false:資料行不需要唯一識別資料列。 如果資料行參與單一或複合主鍵,則此值為 true 。 否則,其值為 false
IsAutoIncrement true:資料行會將值指派給固定增量的新資料列。 false:資料行不會將值指派給固定增量的新資料列。 這個資料行的預設值為 false
BaseCatalogName 包含資料行的資料存放區中之目錄名稱。 Null 如果無法判斷基底目錄名稱,則為 。 此資料行的預設值為 null 值。
BaseSchemaName 此直一律為 Null
BaseTableName DataTable 的名稱。
BaseColumnName 中的資料 DataTable 行名稱。
AutoIncrementSeed DataTableAutoIncrementSeed 的 屬性值。
AutoIncrementStep DataTableAutoIncrementStep 的 屬性值。
DefaultValue DataColumnDefaultValue 的 屬性值。
運算式 如果目前資料行是運算式資料行,且運算式中使用的所有資料行都屬於包含運算式資料行的相同 T:System.Data.DataTable 資料行,則為運算式字串,否則 null 為 。
ColumnMapping MappingTypeDataColumn 相關聯的值。 類型可以是 、 ElementHiddenSimpleContentAttribute 其中一個。 預設值是 Element
BaseTableNamespace DataTableNamespace 的 屬性值。
BaseColumnNamespace DataColumnNamespace 的 屬性值。

適用於