Muokkaa

Jaa


GetSchema and Schema Collections

The Connection classes in each of the .NET Framework managed providers implement a GetSchema method that's used to retrieve schema information about the database that is currently connected. The schema information returned from the GetSchema method comes in the form of a DataTable. The GetSchema method is an overloaded method that provides optional parameters for specifying the schema collection to return, and for restricting the amount of information returned.

Specifying the Schema Collections

The first optional parameter of the GetSchema method is the collection name which is specified as a string. There are two types of schema collections: common schema collections that are common to all providers, and specific schema collections which are specific to each provider.

You can query a .NET Framework managed provider to determine the list of supported schema collections by calling the GetSchema method with no arguments, or with the schema collection name "MetaDataCollections". This will return a DataTable with a list of the supported schema collections, the number of restrictions that they each support, and the number of identifier parts that they use.

Retrieving Schema Collections Example

The following examples demonstrate how to use the GetSchema method of the .NET Framework Data Provider for the SQL Server SqlConnection class to retrieve schema information about all of the tables contained in the AdventureWorks sample database:

Imports System.Data.SqlClient

Module Module1
   Sub Main()
      Dim connectionString As String = GetConnectionString()
      Using connection As New SqlConnection(connectionString)
         'Connect to the database then retrieve the schema information.
         connection.Open()
         Dim table As DataTable = connection.GetSchema("Tables")

         ' Display the contents of the table.
         DisplayData(table)
         Console.WriteLine("Press any key to continue.")
         Console.ReadKey()
      End Using
   End Sub

   Private Function GetConnectionString() As String
      ' To avoid storing the connection string in your code,
      ' you can retrieve it from a configuration file.
      Return "..."
   End Function

   Private Sub DisplayData(ByVal table As DataTable)
      For Each row As DataRow In table.Rows
         For Each col As DataColumn In table.Columns
            Console.WriteLine("{0} = {1}", col.ColumnName, row(col))
         Next
         Console.WriteLine("============================")
      Next
   End Sub
End Module
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
  static void Main()
  {
  string connectionString = GetConnectionString();
  using (SqlConnection connection = new SqlConnection(connectionString))
  {
   // Connect to the database then retrieve the schema information.
   connection.Open();
   DataTable table = connection.GetSchema("Tables");

   // Display the contents of the table.
   DisplayData(table);
   Console.WriteLine("Press any key to continue.");
   Console.ReadKey();
   }
 }

  private static string GetConnectionString()
  {
   // To avoid storing the connection string in your code,
   // you can retrieve it from a configuration file.
   return "...";
  }

  private static void DisplayData(System.Data.DataTable table)
  {
     foreach (System.Data.DataRow row in table.Rows)
     {
        foreach (System.Data.DataColumn col in table.Columns)
        {
           Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
        }
     Console.WriteLine("============================");
     }
  }
}

See also