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 如果數據行本身代表索引鍵,或只有唯一類型的條件約束僅適用於此數據行,則保證為 true。 false:數據行可以包含 中的 DataTable重複值。 這個資料行的預設值為 false
IsKey true:數據行是一組數據行之一,會結合在一起,唯一識別 中的數據 DataTable列。 設定為 true 的數據IsKey行集合必須唯一識別 中的數據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 MappingType與相關聯的DataColumn值。 類型可以是 、ElementHiddenSimpleContentAttribute其中一個。 預設值是 Element
BaseTableNamespace DataTableNamespace 屬性值。
BaseColumnNamespace DataColumnNamespace 屬性值。

適用於