使用 GetOleDbSchemaTable 和 Visual C# .NET 檢索架構信息

如何用 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 成員: 在傳遞限制數組的值時,對於不包含值的數組元素使用 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();
    
    //Pause
    Console.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();
    
    //Pause
    Console.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();
    
    //Pause
    Console.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();
    
    //Pause
    Console.ReadLine();
  4. 修改 ConnectionString,以使用具有足夠權限可列出主鍵的帳戶連接到您的 SQL Server 計算機。
  5. 按 F5 鍵編譯並運行該項目。您會注意到羅斯文數據庫和 Pubs 數據庫的 Employee 表的主鍵已列在控制檯窗口中。
  6. 按 ENTER 鍵結束控制檯應用程序並回到 IDE。
轉載出自:http://support.microsoft.com/kb/309681/zh-cn

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章