March 2008
Intermediate to advanced
982 pages
22h 43m
English
You need to get schema information for the data source of a data connection.
Use the GetSchema() method of the Connection class.
The solution creates a connection to the AdventureWorks database. The following metadata information is retrieved and output to the console to demonstrate the three overloads of the GetSchema() method:
Metadata collections for the connection
Restriction collections for the connection
Views in the Production schema
The C# code in Program.cs in the project RetrieveDatabaseMetadata is shown in Example 11-2.
Example 11-2. File: Program.cs for RetrieveDatabaseMetadata solution
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
namespace RetrieveDatabaseMetadata
{
class Program
{
static void Main(string[] args)
{
string sqlConnectString = "Data Source=(local);" +
"Integrated security=SSPI;Initial Catalog=AdventureWorks;";
using (SqlConnection connection = new SqlConnection(sqlConnectString))
{
// Retrieve and output metadata collections
connection.Open( );
DataTable dt = connection.GetSchema(
DbMetaDataCollectionNames.MetaDataCollections); connection.Close( ); Console.WriteLine("---Metadata collections---"); foreach (DataRow row in dt.Rows) Console.WriteLine("{0}; {1}; {2}", row[0], row[1], row[2]); // Retrieve and output restrictions connection.Open( ); DataTable dtRestrictions = connection.GetSchema(DbMetaDataCollectionNames.Restrictions); connection.Close( ...Read now
Unlock full access