使用 GetOleDbSchemaTable 和 Visual C# .NET 检索架构信息

来源:互联网 发布:高桥和希 知乎 编辑:程序博客网 时间:2024/05/22 12:06
如何用 ADO.NET 中 OleDbConnection 对象的 GetOleDbSchemaTable 方法检索数据库架构信息。数据源中的架构信息包括数据库或可通过数据库中的数据源、表和视图得到的目录以及所存在的约束等。表中的架构信息包括主键、列和自动编号字段。

注意,在使用 SqlClient.SqlConnection 对象时没有与 GetOleDbSchemaTable 等价的方法。SQL Server .NET 数据提供程序通过存储过程和信息性视图展示后端架构信息。有关可通过 Microsoft SQL Server 得到的视图和存储过程的更多信息,请参见 MSDN 库中的 Transact-SQL 参考。 

OleDbConnection 对象的 GetOleDbSchemaTable 方法

OLE DB .NET 数据提供程序使用 OleDbConnection 对象的 GetOleDbSchemaTable 方法展示架构信息。GetOleDbSchemaTable 返回填充了架构信息的 DataTable

GetOleDbSchemaTable 的第一个参数是架构参数,它是一个 OleDbSchemaGuid 类型的标识,指定了要返回的架构信息的类型(如表、列和主键)。第二个参数是一个限制对象数组,对 DataTable 架构中返回的行进行过滤(例如,您可以指定对表的名称、类型、所有者和/或架构的限制)。 

OleDbSchemaGuid 成员

OleDbSchemaGuid 参数指定 GetOleDbSchemaTable 方法要返回的架构表的类型。 OleDbSchemaGuid 成员主要包括:
  • 外键
  • 索引
  • 主键
  • 视图


限制

限制是一个过滤值对象数组,每个元素对应于结果 DataTable 中的一个 DataColumnOleDbSchemaGuid 参数决定了相应的限制。例如,在指定表的 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,因为您不过滤这些对象:
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"});
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 开始的:
for (int i = 0; i < schemaTable.Columns.Count; i++) {Console.WriteLine(schemaTable.Columns[i].ToString());}
若要获得每一列的值(即实际的表名称,如 Categories、Customers 和 Employees),您可以使用该行的 ItemArray 的位置顺序。注意 ItemArray 数组的元素下标是从 0 开始的:
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)。
  1. 启动 Visual Studio .NET。
  2. 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
  3. 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
    using System.Data;using System.Data.OleDb;
  4. 在代码窗口中,将下面的代码粘贴到 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();//PauseConsole.ReadLine();
  5. 修改 ConnectionString,以使用在罗斯文 (Northwind) 数据库中具有列表权限的帐户连接到您的 SQL Server 计算机。
  6. 按 F5 键编译并运行该项目。您会注意到表已列在控制台窗口中。
  7. 按 ENTER 键结束控制台应用程序并回到集成开发环境 (IDE)。

创建检索表的架构的示例

以下示例列出 SQL Server Northwind 数据库中 Employees 表的架构信息。

OleDbSchemaGuid.Tables 将返回那些可由特定登录访问的表(包括视图)。如果指定对象数组 {null, null, "Employees", "TABLE"},那么您的过滤结果只包括名为 Employees 的表。然后列出返回的架构表的架构信息。
  1. 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
  2. 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
    using System.Data;using System.Data.OleDb;
  3. 在代码窗口中,将下面的代码粘贴到 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();//PauseConsole.ReadLine();
  4. 修改 ConnectionString,以使用具有检索 Employees 表架构权限的帐户连接到您的 SQL Server 计算机。
  5. 按 F5 键编译并运行该项目。您会注意到表已列在控制台窗口中。
  6. 按 ENTER 键结束控制台应用程序并回到 IDE。

创建列出表中的列的示例

以下示例列出 SQL Server Northwind 数据库中 Employees 表中的列名称。

OleDbSchemaGuid.Columns 将返回那些可由特定登录访问的表和视图中的列。如果指定对象数组 {null, null, "Employees", null},您的过滤结果只包括 Employees 表中的列。
  1. 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
  2. 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
    using System.Data;using System.Data.OleDb;
  3. 在代码窗口中,将下面的代码粘贴到 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();//PauseConsole.ReadLine();
  4. 修改 ConnectionString,以使用具有列出 Employees 表中各列的权限的帐户连接到您的 SQL Server 计算机。
  5. 按 F5 键编译并运行该项目。您会注意到 Employees 表中的列已列在控制台窗口中。
  6. 按 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;"
因为,罗斯文或 Pubs 数据库将在限制数组的 TABLE_CATALOG 中指定。此代码指定表的所有者"dbo"作为 TABLE_SCHEMA 限制。此外,代码还指定了 TABLE_NAME 限制的表名称。

若要获得罗斯文数据库中 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"});
若要创建示例,可以按照下列步骤进行:
  1. 新建一个 Visual C# 控制台应用程序项目。默认情况下,Class1.cs 将添加到项目中。
  2. 打开 Class1 的代码窗口。将下面的代码粘贴到代码窗口的顶部,位于 namespace 声明之上:
    using System.Data;using System.Data.OleDb;
  3. 在代码窗口中,将下面的代码粘贴到 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();//PauseConsole.ReadLine();
  4. 修改 ConnectionString,以使用具有足够权限可列出主键的帐户连接到您的 SQL Server 计算机。
  5. 按 F5 键编译并运行该项目。您会注意到罗斯文数据库和 Pubs 数据库的 Employee 表的主键已列在控制台窗口中。
  6. 按 ENTER 键结束控制台应用程序并回到 IDE。
转载出自:http://support.microsoft.com/kb/309681/zh-cn

原创粉丝点击