dotConnect for Oracle Documentation
In This Topic
    Retrieving Metadata
    In This Topic
    This feature is not supported for applications, targeting .NET Core 1 or .NET Standard 1.3. It is supported only for full .NET Framework, .NET Core 2.0 and higher, or .NET Standard 2.0 or higher.

    Usually you have to dig through SQL references to find out how to get metadata information for specific DBMS. There are few servers that support same SQL commands. Sometimes the syntax differs slightly, sometimes a server does not support certain statement. Now you can forget about those problems because dotConnect for Oracle retrieves the metadata for you.

    You can take advantage of the new useful feature - GetSchema method. It allows you to read server schema information without writing queries and parsing the output. All information you may want to obtain is brought to you by single function in easy-to-process format. You can get information on schemas, views, packages, tables, columns, indexes, users, procedures and functions, their arguments, and reserved words. The method is introduced in System.Data.Common.DbConnection.

    This article consists of the following sections:

    How To Use

    GetSchema method is available in three overloads, each of them serves its own purpose. All overloads return System.Data.DataTable object that contains information about server elements.

    public virtual abstract DataTable GetSchema();
    
    
    Overloads Public Overridable MustOverride Function GetSchema() As DataTable
    
    

    If you call the GetSchema method without parameters, or with single parameter "MetaDataCollections" (which is actually the same), the table object returned by the method will contain three columns. The first field of every row is a keyword allowed to be passed to the method (as collectionName argument). The second field is the number of restriction values for this keywords (passed through restrictionValues argument). The third field is not used in dotConnect for Oracle. It is always zero.

    public virtual abstract DataTable GetSchema(
       string collectionName
    );
    
    
    Overloads Public Overridable MustOverride Function GetSchema( _
       ByVal collectionName As String _
    ) As DataTable
    
    

    GetSchema with 1 argument returns general information about the collection queried. For example, GetSchema("Users") returns the list of users on the server.

    public virtual abstract DataTable GetSchema(
       string collectionName,
       string[] restrictionValues
    );
    
    
    Overloads Public Overridable MustOverride Function GetSchema( _
       ByVal collectionName As String, _
       ByVal restrictionValues() As String _
    ) As DataTable
    
    

    In this overload first parameter is name of a collection, and second parameter is the array of restrictions to be applied when querying information. These restrictions specify which subset of the collection will be returned. The restrictions can include, for example, the database name (in this case, only collection elements belonging to this database will be returned) or the mask for the name of collection elements (only the elements satisfying this mask will be returned). The quantity and description of restrictions allowed for each metadata collection are represented in the table here. Their number can also be obtained from the return of the GetSchema() method. If the second parameter is null/Nothing, it is ignored.

    Instead of specifying the metadata collection name as a string constant, you may use members of System.Data.DbMetaDataCollectionNames and Devart.Data.Oracle.OracleMetadataCollectionNames as the first GetSchema argument values. The members of these classes are the string fields, each field stores the corresponding metadata collection name. It is recommended to use these fields rather than manually input the collection names manually as the string constants because in case of using these fields, you will find misspellings at compile-time, and intellisense will show you all the available metadata collection names.

    GetSchema Method Reference

    The following table provides detailed information on metadata collections that can be retrieved using the GetSchema method, and restrictions that can be applied for them. Some collections may be not supported in older server versions. If you try to get metadata for unsupported collection you will get exception with message "Collection not defined".

    Collection Name

    Number of restrictions

    Remarks

    MetaDataCollections 0

    Returns this list. Same as using GetSchema() method without parameters.

    ReservedWords 0

    Lists all reserved words used in the server.

    Users 1

    Lists all users on the server.

    When restricted by username, returns information about specific user.

    Tables 2

    GetSchema("Tables") returns the list of all tables on the server that you have access to.

    • The first restriction for this collection is name of a schema. If specified, the method returns all tables within the schema.
    • The second restriction is table name. Note that masks are not allowed in dotConnect for Oracle.
    Views 2

    GetSchema("Views") returns the list of all views on the server that you have access to.

    • The first restriction for this collection is name of a schema. If specified, the method returns all views within the schema.
    • The second restriction is the name of the view.
    Columns 3

    Returns the list of columns, their type and some extra information.
    GetSchema("Columns") returns the list of all columns in all schemas of the table.

    • Restricted by schema name, the method returns all columns in the specified schema.
    • The second restriction is a name of a table that GetSchema method should search in.
    • At last, you can specify column name.
    Indexes 4

    Returns the list of indexes and their details.

    • The first restriction is name of a schema the indexes belongs to.
    • The second restriction is name of the index.
    • The third restriction is name of a schema the table belongs to.
    • The last restriction is name of a table that uses the index.
    IndexColumns 5

    Returns information about columns included in indexes. The following restrictions may be specified:

    • The first restriction is name of the schema for indexes;
    • Index name;
    • Name of the schema for tables;
    • Table name;
    • Column name.
    Functions 2

    Returns the list of functions on the server. The following restrictions may be specified:

    • Schema name;
    • Function name.
    Procedures 3

    Returns the list of procedures on the server. The following restrictions may be specified:

    • Schema name;
    • Package name;
    • Procedure name.
    Arguments* 4

    Returns the list of procedure and function arguments. The following restrictions may be specified:

    • Schema name;
    • Package name;
    • Procedure name;
    • Argument name. 
    Synonyms 2

    Returns the list of synonyms on the server. The following restrictions may be specified:

    • Schema name;
    • Synonym name.
    Sequences 2

    Returns the list of sequences on the server. The following restrictions may be specified:

    • Schema name;
    • Sequence name.
    Packages 2

    Returns the list of packages on the server. The following restrictions may be specified:

    • Schema name;
    • Package name.
    PackageBodies 2

    Returns the list of package bodies on the server that you have access to. The following restrictions may be specified:

    • Schema name;
    • Package name.
    PrimaryKeys 3

    Returns the list of primary keys on the server. The following restrictions may be specified:

    • Schema name;
    • Key name;
    • Table name.
    PrimaryKeyColumns 3

    Returns the list of columns of primary keys on the server. The following restrictions may be specified:

    • Schema name;
    • Key name;
    • Table name;
    • Column name.
    ForeignKeys 3

    Returns the list of foreign keys on the server. The following restrictions may be specified:

    • Schema name;
    • Key name;
    • Table name.
    ForeignKeyColumns 3

    Returns the list of columns of foreign key columns on the server. The following restrictions may be specified:

    • Schema name;
    • Key name;
    • Table name.
    FullForeignKeyColumns 3

    Returns the list of columns of foreign key columns on the server. In comparison to the previous collection, a column with the names of the corresponding referenced columns in the parent table is added to the result DataTable. The following restrictions may be specified:

    • Schema name;
    • Key name;
    • Table name.
    Triggers 2

    Returns the list of triggers on the server that you have access to. The following restrictions may be specified:

    • Schema name;
    • Trigger name.
    Clusters 2

    Returns the list of clusters on the server that you have access to. The following restrictions may be specified:

    • Schema name;
    • Cluster name.
    QueuePublishers 3

    Returns the list of queue publishers on the server. The following restrictions may be specified:

    • Schema name;
    • Queue name;
    • Publisher name.
    Queues 5

    Returns the list of queues on the server. The following restrictions may be specified:

    • Schema name;
    • Queue name;
    • Queue table name;
    • Queue payload type;
    • Queue payload object type.
    QueueTables 4

    Returns the list of queue tables on the server. The following restrictions may be specified:

    • Schema name;
    • Queue table name;
    • Queue payload type;
    • Queue payload object type.
    QueueSubscribers 4

    Returns the list of subscribers to a queue or queues on the server. The following restrictions may be specified:

    • Schema name;
    • Queue name;
    • Queue table name;
    • Subscriber name.

    * For the Arguments collection, you can get arguments only from package stored routines or vice versa using the package name restriction easily:

    Samples

    The following code fragment is an elegant way to detect existence of a table.

    string tableName = "DEPT";
    if (myDbConnection.GetSchema("Tables", new string[] { "SCOTT", tableName }).Rows.Count > 0)
    {
    Console.WriteLine("Table " + tableName + " found.");
    }
    
    
    Dim tableName As String = "DEPT"
    Dim restrictions() As String = {"SCOTT", tableName}
    If (myDbConnection.GetSchema("Tables", restrictions).Rows.Count > 0) Then
      Console.WriteLine("Table " + tableName + " found.")
    End If
    
    

    The next sample shows how to retrieve columns information from a table and render it to console.

    static void GetTableInfo(OracleConnection oraConnection, string tableName)
    {
      oraConnection.Open();
      DataTable myDataTable = oraConnection.GetSchema("Columns", new string[] { 
                "SCOTT", tableName });
      for (int i = 0; i < myDataTable.Columns.Count; i++)
      {
        Console.Write(myDataTable.Columns[i].Caption + "\t");
      }
      Console.WriteLine();
      foreach (DataRow myRow in myDataTable.Rows)
      {
        foreach (DataColumn myCol in myDataTable.Columns)
        {
          Console.Write(myRow[myCol] + "\t");
        }
        Console.WriteLine();
      }
      oraConnection.Close();
    }
    
    
    Public Sub GetTableInfo(ByVal oraConnection As OracleConnection, ByVal tableName As String)
      oraConnection.Open()
      Dim restrictions() As String = {"SCOTT", tableName}
      Dim myDataTable As DataTable = oraConnection.GetSchema("Columns", restrictions)
      Dim i As Int32
      For i = 0 To myDataTable.Columns.Count - 1
        Console.Write(myDataTable.Columns(i).Caption & Chr(9))
      Next
      Console.WriteLine()
      Dim myRow As DataRow
      Dim myCol As DataColumn
      For Each myRow In myDataTable.Rows
        For Each myCol In myDataTable.Columns
          Console.Write(myRow(myCol) & Chr(9))
        Next
        Console.WriteLine()
      Next
      oraConnection.Close()
    End Sub
    
    

    The following sample demonstrates how to generate SQL CREATE TABLE statement basing on metadata retrieved with GetSchema method. Only column name and type are included in the script.

    static void GetCreateTable(OracleConnection oraConnection, string tableName)
    {
      //Open the connection
      oraConnection.Open();
      //Fill DataTable with columns information
      DataTable myDataTable = oraConnection.GetSchema("Columns", new string[] { 
                                                       "SCOTT", tableName });
      string queryText = "CREATE TABLE " + tableName + " (\n";
      string fieldLine;
      DataRow myRow;
      //For every row in the table
      for (int i = 0; i < myDataTable.Rows.Count; i++)
      {
        //Get column name and type
        myRow = myDataTable.Rows[i];
        fieldLine = myRow["Name"] + " " + myRow["DataType"];
        //Add column size if required
        if (Array.IndexOf(new string[] { 
    	   "NUMBER", "VARCHAR", "VARCHAR2", "RAW" 
    	 }, myRow["DataType"]) != -1)
        {
          fieldLine = fieldLine + "(" + myRow["Length"] + ")";
        }
        //Add coma or closing bracket
        if (i < myDataTable.Rows.Count - 1)
        {
          fieldLine = fieldLine + ",\n";
        }
        else
        {
          fieldLine = fieldLine + ")";
        }
        //Add new column to script
        queryText = queryText + fieldLine;
      }
      Console.WriteLine(queryText);
      //Close the connection
      oraConnection.Close();
    }
    
    
    Public Sub GetCreateTable(ByVal oraConnection As OracleConnection, _ 
      ByVal tableName As String)
      'Open the connection
      oraConnection.Open()
      Dim restrictions() As String = {"SCOTT", tableName}
      'Fill DataTable with columns information
      Dim myDataTable As DataTable = oraConnection.GetSchema("Columns", restrictions)
      Dim queryText As String = "CREATE TABLE " + tableName & _
                                + " (" + System.Environment.NewLine
      Dim fieldLine As String
      Dim myRow As DataRow
      Dim i As Int32
      'For every row in the table
      For i = 0 To myDataTable.Rows.Count - 1
        'Get column name and type
        myRow = myDataTable.Rows(i)
        fieldLine = myRow("Name") & " " & myRow("DataType")
        'Add column size if required
        If (Array.IndexOf(New String() { _
    	  "VARCHAR", "VARCHAR2", "RAW", "NUMBER"}, myRow("DataType")) <> -1) Then
          fieldLine = fieldLine & "(" & myRow("Length") & ")"
        End If
        'Add coma or closing bracket
        If (i < myDataTable.Rows.Count - 1) Then
          fieldLine = fieldLine + "," + System.Environment.NewLine
        Else
          fieldLine = fieldLine + ")"
        End If
        'Add new column to script
        queryText = queryText + fieldLine
      Next
      Console.WriteLine(queryText)
      'Close the connection
      oraConnection.Close()
    End Sub
    
    

    Also you can get a metadata of query result set using the GetSchemaTable method of OracleDataReader and the ShemaTable property of OracleDataTable classes.

    See Also

    OracleConnection Class | DbConnectionBase.GetSchema Method