本文演示如何用 ADO.NET 中 OleDbConnection 对象的 GetOleDbSchemaTable 方法检索数据库架构信息。数据源中的架构信息包括数据库或可通过数据库中的数据源、表和视图得到的目录以及所存在的约束等。表中的架构信息包括主键、列和自动编号字段。
注意,在使用 SqlClient.SqlConnection 对象时没有与 GetOleDbSchemaTable 等价的方法。SQL Server .NET 数据提供程序通过存储过程和信息性视图展示后端架构信息。有关可通过 Microsoft SQL Server 得到的视图和存储过程的更多信息,请参见 MSDN 库中的 Transact-SQL 参考。
•列
•外键
•索引
•主键
•表
•视图
有关 OleDbSchemaGuid 成员的完整列表,请参见参考部分的"OleDbSchemaGuid Members"Web 站点。
{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 (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"});
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 的其他架构列。
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());
}
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();
//Pause
Console.ReadLine();
5.
修改 ConnectionString,以使用在罗斯文 (Northwind) 数据库中具有列表权限的帐户连接到您的 SQL Server 计算机。
6.
按 F5 键编译并运行该项目。您会注意到表已列在控制台窗口中。
7.
按 ENTER 键结束控制台应用程序并回到集成开发环境 (IDE)。
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();
//Pause
Console.ReadLine();
4.
修改 ConnectionString,以使用具有检索 Employees 表架构权限的帐户连接到您的 SQL Server 计算机。
5.
按 F5 键编译并运行该项目。您会注意到表已列在控制台窗口中。
6.
按 ENTER 键结束控制台应用程序并回到 IDE。
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();
//Pause
Console.ReadLine();
4.
修改 ConnectionString,以使用具有列出 Employees 表中各列的权限的帐户连接到您的 SQL Server 计算机。
5.
按 F5 键编译并运行该项目。您会注意到 Employees 表中的列已列在控制台窗口中。
6.
按 ENTER 键结束控制台应用程序并回到 IDE。
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=sa;
Password=password;"
因为,罗斯文或 Pubs 数据库将在限制数组的 TABLE_CATALOG 中指定。此代码指定表的所有者"dbo"作为 TABLE_SCHEMA 限制。此外,代码还指定了 TABLE_NAME 限制的表名称。
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys,
new Object[] {"Northwind", "dbo", "Employees"});
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();
//Pause
Console.ReadLine();
4.
修改 ConnectionString,以使用具有足够权限可列出主键的帐户连接到您的 SQL Server 计算机。
5.
按 F5 键编译并运行该项目。您会注意到罗斯文数据库和 Pubs 数据库的 Employee 表的主键已列在控制台窗口中。
6.
按 ENTER 键结束控制台应用程序并回到 IDE。