Article ID: 309681 - Last Review: July 1, 2004 - Revision: 3.4 How To Retrieve Schema Information by Using GetOleDbSchemaTable and Visual C# .NETThis article was previously published under Q309681 On This PageSUMMARY This article demonstrates how to use the GetOleDbSchemaTable method of the OleDbConnection object in ADO.NET to retrieve database schema information. Schema
information in a data source includes databases or catalogs that are available
from the data source, tables, and views in a database, as well as constraints
that exist and so on. Schema information in a table includes primary keys,
columns, and autonumber fields. Note that no method is equivalent to GetOleDbSchemaTable when you use a SqlClient.SqlConnection object. The SQL Server .NET Data Provider exposes backend schema information through stored procedures and informational views. For more information about views and stored procedures that are available through Microsoft SQL Server, see the Transact-SQL reference in the MSDN Library. RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
GetOleDbSchemaTable Method of the OleDbConnection ObjectThe OLE DB .NET Data Provider uses the GetOleDbSchemaTable method of the OleDbConnection object to expose schema information. GetOleDbSchemaTable returns a DataTable that is populated with the schema information.The first argument of GetOleDbSchemaTable is the schema parameter, an OleDbSchemaGuid argument that identifies which schema information to return (such as tables, columns, and primary keys). The second argument is an Object array of restrictions to filter the rows that are returned in the schema DataTable (for example, you may specify restrictions for table name, type, owner, and /or schema). OleDbSchemaGuid MembersThe OleDbSchemaGuid argument specifies the type of schema table for the GetOleDbSchemaTable method. Some of the OleDbSchemaGuid members include the following:
RestrictionsRestrictions are an Object array of filter values, each of which corresponds to a DataColumn in the resulting DataTable. The OleDbSchemaGuid argument determines the relevant restrictions. For example, when you specify an OleDbSchemaGuid of tables, the array of restrictions is as follows:
{TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE}
To review the available restrictions, click any of the OleDbSchemaGuid members at the following Microsoft Web site:
OleDbSchemaGuid Members When you pass values in the restriction array, include the Visual
C# .NET null keyword for array elements that do not contain values. For
example, if you want to retrieve table schemas, use OleDbSchemaGuid.Tables. However, if you specify tables, this also returns aliases,
synonyms, views, and other related objects. Thus, if you want to filter out all
objects except tables, use a restriction of TABLE for TABLE_TYPE. You can use null for TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME because you are
not filtering on these objects: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp) Returned DataTableThe DataTable that the GetOleDbSchemaTable method returns has a row for each object that meets the OleDbSchemaGuid type and the restriction criteria. The DataTable has a column for each of the restriction columns, which is followed by additional schema information based on the OleDbSchemaGuid field.For example, when you use the following code, each row in the returned DataTable is a database table: To obtain this list of column names (that is, the field descriptors, such as TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME), you can use the ordinal position of the columns. Note that the Columns array is zero-based: Create Sample That List Tables in a DatabaseThe following sample lists tables in the SQL Server Northwind database.OleDbSchemaGuid.Tables returns those tables (including views) that are accessible to a given log on. If you specify an Object array of {null, null, null, "TABLE"}, you filter to include only a TABLE_TYPE of TABLE. You then list the table name (TABLE_NAME) of each row in the returned schema table.
Create Sample That Retrieves Schema for a TableThe following sample lists schema information for the Employees table in the SQL Server Northwind database.OleDbSchemaGuid.Tables returns those tables (including views) that are accessible to a given log on. If you specify an Object array of {null, null, "Employees", "TABLE"}, you filter to include only a table named Employees. You then list the schema information for the returned schema table.
Create Sample That Lists Columns in a TableThe following sample lists the names of columns in the Employees table in the SQL Server Northwind database.OleDbSchemaGuid.Columns returns those columns in tables and views that are accessible to a given log on. If you specify an Object array of {null, null, "Employees", null}, you filter to include only the columns for the Employees table.
Create Sample That Lists Primary Keys in a TableThe following sample lists the primary keys of the Employees table in the SQL Server Northwind database and of the Employee table in the SQL Server Pubs database.OleDbSchemaGuid.Primary_Keys returns those primary keys in a catalog that are accessible to a given log on. In this sample, the OleDbConnection is to SQL Server but not to a specific SQL Server database: To obtain the primary key of the Employees table in the Northwind database, you use an Object array of {"Northwind", "dbo", "Employees"}:
REFERENCES For a complete list of the OleDbSchemaGuid members, refer to the following topic in the Microsoft .NET
Framework Software Development Kit (SDK) documentation. For a list of available
restrictions, click any of the OleDbSchemaGuid members:
OleDbSchemaGuid Members For more information, refer to the following topics in the .NET
Framework SDK documentation: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid_members.aspx (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid_members.aspx)
Obtaining Schema Information from a Database http://msdn.microsoft.com/en-us/library/kcax58fh.aspx (http://msdn.microsoft.com/en-us/library/kcax58fh.aspx) OleDbConnection.GetOleDbSchemaTable Method http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.getoledbschematable.aspx (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.getoledbschematable.aspx) OleDbSchemaGuid.Tables Field http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid.tables.aspx (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid.tables.aspx) OleDbSchemaGuid.Columns Field http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid.columns.aspx (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid.columns.aspx) OleDbSchemaGuid.Primary_Keys Field http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid.primary_keys.aspx (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid.primary_keys.aspx) | Article Translations
|
Back to the top
