日本語PDF

GetSchema(string collectionName, string[] restrictions)

このメソッドは、コレクション名に指定された文字列および制限値に指定された文字列配列を使用して、OracleConnectionのデータ・ソースのスキーマ情報を戻します。

宣言

// C#
public override DataTable GetSchema (string collectionName, 
    string[] restrictions);
 

パラメータ

  • collectionName

    取得されるメタデータのコレクション名。

  • restrictions

    取得されるメタデータに適用される制限の配列。

戻り値

DataTableオブジェクト。

例外

  • ArgumentException - 要求されたコレクションが定義されていません。

  • InvalidOperationException - 次の条件の1つが存在します。

    • 接続がクローズされています。

    • 要求されたコレクションは現行バージョンのOracle Databaseではサポートされていません。

    • 要求されたコレクションがサポートしているよりも多くの制限が指定されています。

    • 要求されたコレクションに対して移入文字列が指定されていません。

備考

このメソッドは、メタデータ・コレクションの名前と、戻されたDataTable内の行をフィルタする制限を指定する文字列値の配列を取得します。これは、指定された制限と一致する指定されたメタデータ・コレクションからの行のみが含まれるDataTableを戻します。

たとえば、Columnsコレクションに3つの制限(ownertablenameおよびcolumnname)がある場合、スキーマにかかわらずEMP表に対するすべての列を取得するには、GetSchemaメソッドで最低でも値nullとEMPを渡す必要があります。

制限値が何も渡されない場合、その制限にはNullを渡すことと同じデフォルト値が使用されます。これは、パラメータ値に空の文字列を渡すこととは異なります。この場合、空の文字列("")は指定されたパラメータの値を考慮します。

collectionNameでは大文字小文字は区別されませんが、制限(文字列値)では区別されます。

// C#
 
using System;
using System.Data;
using System.Data.Common;
using Oracle.DataAccess.Client;
 
class GetSchemaSample
{
  static void Main(string[] args)
  {
    string constr = "User Id=scott; Password=tiger; Data Source=oracle;";
    string ProviderName = "Oracle.DataAccess.Client";
 
    DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
 
    using (DbConnection conn = factory.CreateConnection())
    {
      try
      {
        conn.ConnectionString = constr;
        conn.Open();
 
        //Get Restrictions
        DataTable dtRestrictions =
          conn.GetSchema(DbMetaDataCollectionNames.Restrictions);
        
        DataView dv = dtRestrictions.DefaultView;
 
        dv.RowFilter = "CollectionName = 'Columns'";
        dv.Sort = "RestrictionNumber";
 
        for (int i = 0; i < dv.Count; i++)
          Console.WriteLine("{0} (default) {1}" , 
                            dtRestrictions.Rows[i]["RestrictionName"], 
                            dtRestrictions.Rows[i]["RestrictionDefault"]);
 
        //Set restriction string array
        string[] restrictions = new string[3];
 
        //Get all columns from all tables owned by "SCOTT"
        restrictions[0] = "SCOTT";
        DataTable dtAllScottCols = conn.GetSchema("Columns", restrictions);
 
        // clear collection
        for (int i = 0; i < 3; i++)
          restrictions[i] = null;
 
        //Get all columns from all tables named "EMP" owned by any 
        //owner/schema
        restrictions[1] = "EMP";
        DataTable dtAllEmpCols = conn.GetSchema("Columns", restrictions);
 
        // clear collection
        for (int i = 0; i < 3; i++)
          restrictions[i] = null;
 
        //Get columns named "EMPNO" from tables named "EMP", 
        //owned by any owner/schema
        restrictions[1] = "EMP";
        restrictions[2] = "EMPNO";
        DataTable dtAllScottEmpCols = conn.GetSchema("Columns", restrictions);
 
        // clear collection
        for (int i = 0; i < 3; i++)
          restrictions[i] = null;
 
        //Get columns named "EMPNO" from all
        //tables, owned by any owner/schema
        restrictions[2] = "EMPNO";
        DataTable dtAllEmpNoCols = conn.GetSchema("Columns", restrictions);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.Source);
      }
    }
  }
}