本文的发布号曾为 CHS309681
本页
概要
本文演示如何用 ADO.NET 中
OleDbConnection 对象的
GetOleDbSchemaTable 方法检索数据库架构信息。数据源中的架构信息包括数据库或可通过数据库中的数据源、表和视图得到的目录以及所存在的约束等。表中的架构信息包括主键、列和自动编号字段。
注意,在使用 SqlClient.SqlConnection 对象时没有与 GetOleDbSchemaTable 等价的方法。SQL Server .NET 数据提供程序通过存储过程和信息性视图展示后端架构信息。有关可通过 Microsoft SQL Server 得到的视图和存储过程的更多信息,请参见 MSDN 库中的 Transact-SQL 参考。
GetOleDbSchemaTable 的第一个参数是架构参数,它是一个 OleDbSchemaGuid 类型的标识,指定了要返回的架构信息的类型(如表、列和主键)。第二个参数是一个限制对象数组,对 DataTable 架构中返回的行进行过滤(例如,您可以指定对表的名称、类型、所有者和/或架构的限制)。
例如,当您使用以下代码时,返回的 DataTable 的每一行是一个数据库表:
DataTable 中返回的每一列是限制列(TABLE_CATALOG、TABLE_SCHEMA、TABLE_NAME、TABLE_TYPE),后面是 TABLE_GUID、DESCRIPTION、TABLE_PROPID、DATE_CREATED 和 DATE_MODIFIED 的其他架构列。
若要获得列名称的列表(即字段描述符,如 TABLE_CATALOG、TABLE_SCHEMA 和 TABLE_NAME),您可以使用列的位置顺序。注意 Columns 数组的元素下标是从 0 开始的:
若要获得每一列的值(即实际的表名称,如 Categories、Customers 和 Employees),您可以使用该行的
ItemArray 的位置顺序。注意
ItemArray 数组的元素下标是从 0 开始的:
OleDbSchemaGuid.Tables 将返回那些可由特定登录访问的表(包括视图)。如果指定对象数组 {null, null, null, "TABLE"},那么您的过滤结果只包括 TABLE 的 TABLE_TYPE。然后在返回的架构表中的每一行列出表名称 (TABLE_NAME)。
OleDbSchemaGuid.Tables 将返回那些可由特定登录访问的表(包括视图)。如果指定对象数组 {null, null, "Employees", "TABLE"},那么您的过滤结果只包括名为 Employees 的表。然后列出返回的架构表的架构信息。
OleDbSchemaGuid.Columns 将返回那些可由特定登录访问的表和视图中的列。如果指定对象数组 {null, null, "Employees", null},您的过滤结果只包括 Employees 表中的列。
OleDbSchemaGuid.Primary_Keys 将返回那些可由特定登录访问的目录中的主键。在此示例中, OleDbConnection 连接到 SQL Server,而不是连接到特定的 SQL Server 数据库:
因为,罗斯文或 Pubs 数据库将在限制数组的 TABLE_CATALOG 中指定。此代码指定表的所有者"dbo"作为 TABLE_SCHEMA 限制。此外,代码还指定了 TABLE_NAME 限制的表名称。
若要获得罗斯文数据库中 Employees 表的主键,您可以使用 {"Northwind", "dbo", "Employees"} 对象数组:
若要获得 Pubs 数据库中 Employee 表的主键,您可以使用 {"Pubs", "dbo", "Employee"} 对象数组:
若要创建示例,可以按照下列步骤进行:
注意,在使用 SqlClient.SqlConnection 对象时没有与 GetOleDbSchemaTable 等价的方法。SQL Server .NET 数据提供程序通过存储过程和信息性视图展示后端架构信息。有关可通过 Microsoft SQL Server 得到的视图和存储过程的更多信息,请参见 MSDN 库中的 Transact-SQL 参考。
要求
下面的列表列出了推荐使用的硬件、软件、网络基础结构以及所需的服务包:- Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server 或 Windows NT 4.0 Server
- Microsoft Visual Studio .NET
- Visual Studio .NET
- ADO.NET 基础和语法
OleDbConnection 对象的 GetOleDbSchemaTable 方法
OLE DB .NET 数据提供程序使用 OleDbConnection 对象的 GetOleDbSchemaTable 方法展示架构信息。 GetOleDbSchemaTable 返回填充了架构信息的 DataTable。GetOleDbSchemaTable 的第一个参数是架构参数,它是一个 OleDbSchemaGuid 类型的标识,指定了要返回的架构信息的类型(如表、列和主键)。第二个参数是一个限制对象数组,对 DataTable 架构中返回的行进行过滤(例如,您可以指定对表的名称、类型、所有者和/或架构的限制)。
OleDbSchemaGuid 成员
OleDbSchemaGuid 参数指定 GetOleDbSchemaTable 方法要返回的架构表的类型。 OleDbSchemaGuid 成员主要包括:- 列
- 外键
- 索引
- 主键
- 表
- 视图
限制
限制是一个过滤值对象数组,每个元素对应于结果 DataTable 中的一个 DataColumn。 OleDbSchemaGuid 参数决定了相应的限制。例如,在指定表的 OleDbSchemaGuid 时,限制数组如下所示:
{TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE}
若要查看可用的限制,请单击以下 Microsoft Web 站点中的任一
OleDbSchemaGuid 成员:
OleDbSchemaGuid 成员
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp
在传递限制数组的值时,对于不包含值的数组元素使用 Visual C# .NET 的
null 关键字。例如,如果要检索表的架构,使用
OleDbSchemaGuid.Tables。但是,如果指定了表,也将返回别名、同义词、视图和其他相关对象。因此,如果您希望过滤掉除表以外的所有其他对象,请对 TABLE_TYPE 使用 TABLE 限制。可以对 TABLE_CATALOG、TABLE_SCHEMA 和 TABLE_NAME 使用
null,因为您不过滤这些对象:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, null, "TABLE"});
返回的数据表
每个符合 OleDbSchemaGuid 类型和限制规则的对象都对应于 GetOleDbSchemaTable 方法返回的 DataTable 中的一行。每个限制列对应于 DataTable 的一列,后面是基于 OleDbSchemaGuid 字段的其他架构信息。例如,当您使用以下代码时,返回的 DataTable 的每一行是一个数据库表:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, null, "TABLE"});
若要获得列名称的列表(即字段描述符,如 TABLE_CATALOG、TABLE_SCHEMA 和 TABLE_NAME),您可以使用列的位置顺序。注意 Columns 数组的元素下标是从 0 开始的:
for (int i = 0; i < schemaTable.Columns.Count; i++) { Console.WriteLine(schemaTable.Columns[i].ToString()); }
for (int i = 0; i < schemaTable.Rows.Count; i++) { Console.WriteLine(schemaTable.Rows[i].ItemArray[2].ToString()); }
创建列出数据库中的表的示例
以下示例列出 SQL Server Northwind 数据库中的表。OleDbSchemaGuid.Tables 将返回那些可由特定登录访问的表(包括视图)。如果指定对象数组 {null, null, null, "TABLE"},那么您的过滤结果只包括 TABLE 的 TABLE_TYPE。然后在返回的架构表中的每一行列出表名称 (TABLE_NAME)。
- 启动 Visual Studio .NET。
- 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
- 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
using System.Data; using System.Data.OleDb;
- 在代码窗口中,将下面的代码粘贴到 Main 函数中:
OleDbConnection cn = new OleDbConnection(); DataTable schemaTable; //Connect to the Northwind database in SQL Server. //Be sure to use an account that has permission to list tables. cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=sa; Password=password;Initial Catalog=Northwind"; cn.Open(); //Retrieve schema information about tables. //Because tables include tables, views, and other objects, //restrict to just TABLE in the Object array of restrictions. schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, null, "TABLE"}); //List the table name from each row in the schema table. for (int i = 0; i < schemaTable.Rows.Count; i++) { Console.WriteLine(schemaTable.Rows[i].ItemArray[2].ToString()); } //Explicitly close - don't wait on garbage collection. cn.Close(); //Pause Console.ReadLine();
- 修改 ConnectionString,以使用在罗斯文 (Northwind) 数据库中具有列表权限的帐户连接到您的 SQL Server 计算机。
- 按 F5 键编译并运行该项目。您会注意到表已列在控制台窗口中。
- 按 ENTER 键结束控制台应用程序并回到集成开发环境 (IDE)。
创建检索表的架构的示例
以下示例列出 SQL Server Northwind 数据库中 Employees 表的架构信息。OleDbSchemaGuid.Tables 将返回那些可由特定登录访问的表(包括视图)。如果指定对象数组 {null, null, "Employees", "TABLE"},那么您的过滤结果只包括名为 Employees 的表。然后列出返回的架构表的架构信息。
- 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
- 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
using System.Data; using System.Data.OleDb;
- 在代码窗口中,将下面的代码粘贴到 Main 函数中:
OleDbConnection cn = new OleDbConnection(); DataTable schemaTable; //Connect to the Northwind database in SQL Server. //Be sure to use an account that has permission to retrieve table schema. cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=sa; Password=password;Initial Catalog=Northwind"; cn.Open(); //Retrieve schema information about the Employees table. schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, "Employees", "TABLE"}); //List the schema info for the Employees table //in the format Field Descriptor :Field Value. for (int i = 0; i < schemaTable.Columns.Count; i++) { Console.WriteLine(schemaTable.Columns[i].ToString() + " : " + schemaTable.Rows[0][i].ToString()); } //Explicitly close - don't wait on garbage collection. cn.Close(); //Pause Console.ReadLine();
- 修改 ConnectionString,以使用具有检索 Employees 表架构权限的帐户连接到您的 SQL Server 计算机。
- 按 F5 键编译并运行该项目。您会注意到表已列在控制台窗口中。
- 按 ENTER 键结束控制台应用程序并回到 IDE。
创建列出表中的列的示例
以下示例列出 SQL Server Northwind 数据库中 Employees 表中的列名称。OleDbSchemaGuid.Columns 将返回那些可由特定登录访问的表和视图中的列。如果指定对象数组 {null, null, "Employees", null},您的过滤结果只包括 Employees 表中的列。
- 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
- 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
using System.Data; using System.Data.OleDb;
- 在代码窗口中,将下面的代码粘贴到 Main 函数中:
OleDbConnection cn = new OleDbConnection(); DataTable schemaTable; //Connect to the Northwind database in SQL Server. //Be sure to use an account that has permission to list the columns in the Employees table. cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=sa; Password=password;Initial Catalog=Northwind"; cn.Open(); //Retrieve schema information about columns. //Restrict to just the Employees TABLE. schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new Object[] {null, null, "Employees", null}); //List the column name from each row in the schema table. for (int i = 0; i < schemaTable.Rows.Count; i++) { Console.WriteLine(schemaTable.Rows[i].ItemArray[3].ToString()); } //Explicitly close - don't wait on garbage collection. cn.Close(); //Pause Console.ReadLine();
- 修改 ConnectionString,以使用具有列出 Employees 表中各列的权限的帐户连接到您的 SQL Server 计算机。
- 按 F5 键编译并运行该项目。您会注意到 Employees 表中的列已列在控制台窗口中。
- 按 ENTER 键结束控制台应用程序并回到 IDE。
创建列出表中的主键的示例
以下示例列出 SQL Server Northwind 数据库的 Employees 表和 SQL Server Pubs 数据库的 Employee 表中的主键。OleDbSchemaGuid.Primary_Keys 将返回那些可由特定登录访问的目录中的主键。在此示例中, OleDbConnection 连接到 SQL Server,而不是连接到特定的 SQL Server 数据库:
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=sa; Password=password;"
若要获得罗斯文数据库中 Employees 表的主键,您可以使用 {"Northwind", "dbo", "Employees"} 对象数组:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, new Object[] {"Northwind", "dbo", "Employees"});
若要获得 Pubs 数据库中 Employee 表的主键,您可以使用 {"Pubs", "dbo", "Employee"} 对象数组:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, new Object[] {"Pubs", "dbo", "Employee"});
- 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
- 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
using System.Data; using System.Data.OleDb;
- 在代码窗口中,将下面的代码粘贴到 Main 函数中:
OleDbConnection cn = new OleDbConnection(); DataTable schemaTable; //Connect to SQL Server. //Be sure to use an account that has permissions to list primary keys //in both the Northwind and Pubs databases. cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=sa; Password=password;"; cn.Open(); //Retrieve schema information about primary keys. //Restrict to just the Employees TABLE in the Northwind CATALOG. schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, new Object[] {"Northwind", "dbo", "Employees"}); //List the primary key for the first row in the schema table. //The first three items in the ItemArray in the row are catalog, schema, and table. //The fourth item is the primary key. Console.WriteLine(schemaTable.Rows[0].ItemArray[3].ToString()); //Retrieve primary key for the Employee TABLE in the Pubs CATALOG. schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, new Object[] {"Pubs", "dbo", "Employee"}); //List the primary key for the first row in the schema table. Console.WriteLine(schemaTable.Rows[0].ItemArray[3].ToString()); //Explicitly close - don't wait on garbage collection. cn.Close(); //Pause Console.ReadLine();
- 修改 ConnectionString,以使用具有足够权限可列出主键的帐户连接到您的 SQL Server 计算机。
- 按 F5 键编译并运行该项目。您会注意到罗斯文数据库和 Pubs 数据库的 Employee 表的主键已列在控制台窗口中。
- 按 ENTER 键结束控制台应用程序并回到 IDE。
参考
有关
OleDbSchemaGuid 成员的完整列表,请参见 Microsoft .NET 框架软件开发工具包 (SDK) 文档资料中的以下主题。有关可用限制的列表,请单击任一
OleDbSchemaGuid 成员:
OleDbSchemaGuid 成员
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidmemberstopic.asp
有关更多信息,请参见 .NET 框架 SDK 文档资料中的以下主题:
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/cpguide/html/cpconobtainingschemainformationfromdatabase.asp
OleDbConnection.GetOleDbSchemaTable 方法
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbconnectionclassgetoledbschematabletopic.asp
OleDbSchemaGuid.Tables 字段
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidclasstablestopic.asp
OleDbSchemaGuid.Columns 字段
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidclasscolumnstopic.asp
OleDbSchemaGuid.Primary_Keys 字段
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidclassprimary_keystopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconobtainingschemainformationfromdatabase.asp
OleDbConnection.GetOleDbSchemaTable 方法
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbconnectionclassgetoledbschematabletopic.asp
OleDbSchemaGuid.Tables 字段
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidclasstablestopic.asp
OleDbSchemaGuid.Columns 字段
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidclasscolumnstopic.asp
OleDbSchemaGuid.Primary_Keys 字段
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbschemaguidclassprimary_keystopic.asp
属性
文章编号: 309681 - 最后修改: 2002年8月13日 - 修订: 1.0
关键字: | kbhowto kbhowtomaster kbsystemdata kboledb KB309681 |
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。