Condividi tramite


GetSchema e raccolte di schemi

Le classi Connection in ognuno dei provider gestiti di .NET Framework implementano un metodo GetSchema che viene usato per recuperare le informazioni dello schema sul database attualmente connesso. Le informazioni sullo schema restituite dal metodo GetSchema sono sotto forma di DataTable. GetSchema è un metodo in overload che fornisce parametri facoltativi per specificare la raccolta di schemi da restituire e limitare la quantità di informazioni restituite.

Specifica di raccolte di schemi

Il primo parametro facoltativo del metodo GetSchema è il nome della raccolta specificato come stringa. Sono disponibili due tipi di raccolte di schemi: raccolte di schemi comuni a tutti i provider e raccolte di schemi specifici, ovvero schemi specifici per ciascun provider.

È possibile eseguire una query in un provider gestito .NET Framework per determinare l'elenco delle raccolte di schemi supportati chiamando il metodo GetSchema senza argomenti oppure con il nome della raccolta di schemi "MetaDataCollections". In questo modo verrà restituito un oggetto DataTable con un elenco delle raccolte di schemi supportati, il numero delle restrizioni supportate da ciascuna raccolta e il numero di parti identificatore usate.

Esempio di recupero di raccolte di schemi

Negli esempi seguenti viene illustrato l'uso del metodo GetSchema del provider di dati .NET Framework per la classe SqlConnection di SQL Server per recuperare informazioni sullo schema relative a tutte le tabelle contenute nel database di esempio AdventureWorks:

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("============================");
     }
  }
}

Vedi anche