???? ID: 309681 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

?????? ??????? ?? GetOleDbSchemaTable ?? ????? C# .NET ?? ????? ???? ??????? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

????????? ???? ?? ?? ???? ?? ?? ????? ???? ????GetOleDbSchemaTable?? ??? ????OleDbConnection???????? ADO.NET ??????? ?????? ??????? ??????? ???? ?? ???? ?????? ??????? ??? ??? ???? ????? ??????? ?? ?????? ???? ?????, ???????? ?? ????? ??? ???? ??????? ?? ?????? ???, ??? ?? ?????? ?? ????? ?? ????? ????? ???? ???????? ?????, ????? ?? autonumber ????? ??? ??? ?????? ?????? ??????? ????? ????

????? ??? ?? ??? ???? ?? ?????? ??GetOleDbSchemaTable?? ?? ?? ????? ????SqlClient.SqlConnection???????? ??? SQL ????? .NET ???? ??????? exposes backend ?????? ??????? ???????? ???????????? ?? ??????? ??????? ?? ?????? ?? ??? ????? ?? ?????? Microsoft SQL Server ?????? ???????? ???????????? ?? ???? ??? ???? ??????? ?? ??? MSDN ????????? ??? Transact SQL ?????? ??????

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Windows 2000 Professional ???, Windows 2000 ?????, Windows 2000 ?????, ?? Windows NT 4.0 ????? ????? ????
  • Microsoft Visual Studio .NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • Visual Studio .NET
  • ADO.NET fundamentals ?? ????????

OleDbConnection ???????? ?? GetOleDbSchemaTable ????

OLE DB .NET ???? ??????? ?? ????? ???? ??GetOleDbSchemaTable?? ??? ????OleDbConnection?????? ??????? ?? ??????? ???? ?? ??? ???????? ???GetOleDbSchemaTable???? ?? ??DataTable?? ?????? ??????? ?? ????????? ???

???? ????GetOleDbSchemaTable?????? ???????? ?? ??OleDbSchemaGuidargument 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 schemaDataTable(for example, you may specify restrictions for table name, type, owner, and /or schema).

OleDbSchemaGuid Members

TheOleDbSchemaGuidargument specifies the type of schema table for theGetOleDbSchemaTable?????? ??? Some of theOleDbSchemaGuidmembers include the following:
  • ?????
  • Foreign keys
  • ???????????
  • Primary keys
  • ????????
  • ?????
For a complete list of theOleDbSchemaGuidmembers, refer to the "OleDbSchemaGuid Members" Web site in the??????????

????????

Restrictions are an Object array of filter values, each of which corresponds to aDataColumnin the resultingDataTable. TheOleDbSchemaGuidargument determines the relevant restrictions. For example, when you specify anOleDbSchemaGuidof tables, the array of restrictions is as follows:
{TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE}
To review the available restrictions, click any of theOleDbSchemaGuidmembers at the following Microsoft Web site:
OleDbSchemaGuid Members
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)
When you pass values in the restriction array, include the Visual C# .NETNULLkeyword for array elements that do not contain values. For example, if you want to retrieve table schemas, useOleDbSchemaGuid.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. ?? ????? ?? ???? ???NULLfor TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME because you are not filtering on these objects:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
              new Object[] {null, null, null, "TABLE"});
				

Returned DataTable

TheDataTablethat theGetOleDbSchemaTablemethod returns has a row for each object that meets theOleDbSchemaGuidtype and the restriction criteria. TheDataTablehas a column for each of the restriction columns, which is followed by additional schema information based on theOleDbSchemaGuid????? ???

?????? ?? ???, ?? ?? ????? ??? ?? ?????, ???????? ?????? ??? ??DataTable??? ??????? ?????? ??:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
              new Object[] {null, null, null, "TABLE"});
				
????? ??? ??? ?? ???DataTable???????? ????? (TABLE_CATALOG TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE), ?? TABLE_GUID, ?????, TABLE_PROPID, DATE_CREATED ?? DATE_MODIFIED ?? ???????? ?????? ??????? ?? ?? ???? ??? ????

????? ?? ??? (??????, ????? ??????????, ???? TABLE_CATALOG, TABLE_SCHEMA ?? TABLE_NAME) ?? ?? ???? ?? ??????? ???? ?? ??? ?? ??????? ?? ??????? ?????? ?? ????? ?? ????? ?? ???????????? zero-based ??:
for (int i = 0; i < schemaTable.Columns.Count; i++) {
   Console.WriteLine(schemaTable.Columns[i].ToString());
}
				
(??????, ???????? ?????? ???, ???? ?????????, ?????? ?? ???????????) ???????? ????? ??? ??? ?? ??????? ???? ?? ??? ?? ??? ??????? ?????? ?? ????? ?? ???? ???ItemArray?? ?????? ??? ?? ???ItemArrayzero-based ??:
for (int i = 0; i < schemaTable.Rows.Count; i++) {
   Console.WriteLine(schemaTable.Rows[i].ItemArray[2].ToString());
}
				

???? ??????? ??? ????? ?? ???? ???????? ?????

????? ????? ??????? ???????? ??? SQL Server ????????? ????????

OleDbSchemaGuid.Tables?? ???????? (????? ????) ??? ?? ??? ?? ???? ?? ??? ????? ????? ??? ?? ???? ??? ??? ?? ????????? {??, ??, ??, "??????"} ?? ??? ???????? ???? ??, ?? ?? ???? ???? TABLE_TYPE ?? ?????? ???????? ???? ?? ??? ??????? ?? ?? ?????? ???? (TABLE_NAME) ?? ??? ?????? ?????? ??? ???????? ?????? ?? ????
  1. Visual Studio .NET ?? ??????? ?????
  2. ????? C# ????? ????????? ??? ??? ????????? ?????? ???????? ??? ?? ????????? ?? ??? Class1.cs ????? ??? ???
  3. Class1 ?? ??? ??? ????? ?? ?????? ??? ??? ????? ?? ???? ??? ??? ????? ??? ?????????? ?????declaration:
    using System.Data;
    using System.Data.OleDb;
    					
  4. ??? ????? ??? ????? ??? ?????????????????? ?? ??? ???? ???.:
    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=<username>;
                           Password=<strong 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????????? ??????? ???? ??? ???????? ?? ?????? ?? ???? ?? ??? ???? SQL Server ???????? ?? ?????? ?????
  6. ?????? ???? ?? ????????? ?? ????? ?? ??? F5 ????? ?????? ????? ??? ?? ???????? ?????? ????? ??? ???????? ????
  7. ????? ????????? ?? ?????? ???? ?? ??? ?????? ????????? ??????? (IDE) ?? ???? ???? ?? ??? ENTER ??????

????? ?? ???? ?????? ?? ??? ?????? ?? Retrieves ?????

????? ????? ?? ??????????? ?? ??? ?????? ??????? ???????? ?????? SQL Server ????????? ??????? ????

OleDbSchemaGuid.Tables?? ???????? (????? ????) ??? ?? ??? ?? ???? ?? ??? ????? ????? ??? ?? ???? ??? ??? ???? ?????? ????????? ???????? ???? ?? {??, ??, "???????????", "??????"}, ???? ?? ?????? ??????????? ?? ??? ????? ???? ?? ??? ??????? ?? ???? ?? ??? ?????? ?????? ?? ??? ?????? ??????? ???
  1. ????? C# ????? ????????? ??? ??? ????????? ?????? ???????? ??? ?? ????????? ?? ??? Class1.cs ????? ??? ???
  2. Class1 ?? ??? ??? ????? ?? ?????? ??? ??? ????? ?? ???? ??? ??? ????? ??? ?????????? ?????declaration:
    using System.Data;
    using System.Data.OleDb;
    					
  3. ??? ????? ??? ????? ??? ?????????????????? ?? ??? ???? ???.:
    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=<username>;
                          Password=<strong 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??????????? ?? ?????? ?????? ??????? ???? ?? ??? ?????? ???? ???? ???? ?? ??? ???? SQL Server ???????? ?? ?????? ?????
  5. ?????? ???? ?? ????????? ?? ????? ?? ??? F5 ?????? ????? ??? ?? ???????? ?????? ????? ??? ???????? ????
  6. ????? ????????? ??? ???? ?? IDE ?? ???? ???? ?? ??? ENTER ??????

???? ?????? ??? ????? ?? ??????? ?? ????? ?????

????? ????? ??????? ??? SQL Server ????????? ??????? ?????? ??????????? ??? ????? ?? ????

OleDbSchemaGuid.Columns?? ??????? ?? ???????? ?? ??? ?? ??? ?? ???? ?? ??? ????? ????? ????? ??? ???? ??? ??? ?? ????????? {??, ??, "???????????", ??} ?? ??? ???????? ???? ??, ??????????? ?? ?????? ?? ??? ???? ????? ???????? ???? ?? ??? ?????? ???
  1. ????? C# ????? ????????? ??? ??? ????????? ?????? ???????? ??? ?? ????????? ?? ??? Class1.cs ????? ??? ???
  2. Class1 ?? ??? ??? ????? ?? ?????? ??? ??? ????? ?? ???? ??? ??? ????? ??? ?????????? ?????declaration:
    using System.Data;
    using System.Data.OleDb;
    					
  3. ??? ????? ??? ????? ??? ?????????????????? ?? ??? ???? ???.:
    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=<username>;
                           Password=<strong 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???? ?? ??? ???? SQL Server ???????? ?? ?????? ???? ?? ??? ?? ??? ?????? ???? ??? ????? ?? ??? ??????????? ?? ?????? ??? ???
  5. ?????? ???? ?? ????????? ?? ????? ?? ??? F5 ?????? ????? ??? ?? ??????????? ?? ?????? ?? ????? ?????? ????? ??? ???????? ????
  6. ????? ????????? ??? ???? ?? IDE ?? ???? ???? ?? ??? ENTER ??????

???? ?????? ??? ???????? ???????? ??????? ?? ????? ?????

??????????? ?? ???????? ????? ?????? SQL Server ????????? ??? ????? ????? ??????? ??????? ?? ???? ?? ?????? SQL Server Pubs ??????? ??? ???

OleDbSchemaGuid.Primary_Keys??? ?????? ?? ??? ?? ??? ?? ???? ?? ??? ????? ????? ??? ?? ?? ???????? ????? ???? ??? ?? ????? ???OleDbConnectionSQL ????? ?? ???? ??????? SQL Server ??????? ?? ??? ???? ?? ????:
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=<username>;
                       Password=<strong password>;"
				
???? ????, ?? ??? ????? ????????? ?? Pubs ??????? ??? ???????? ???? TABLE_CATALOG ?? ??? ???? ?? ??? ?????? ??????, "dbo", TABLE_SCHEMA ???????? ?? ??? ????????? ???? ??? ???? ?????, ?? ??? TABLE_NAME ???????? ?? ??? ?????? ??? ????????? ???? ???

????????? ??????? ??? ??????????? ?? ?????? ?? ???????? ????? ?? ??????? ???? ?? ??? ?? {"?????????", "dbo", "???????????"} ?? ??? ???????? ???? ?? ????? ????:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, 
              new Object[] {"Northwind", "dbo", "Employees"});
				
Pubs ??????? ??? ???? ?????? ?? ???????? ????? ?? ??????? ???? ?? ??? ?? {"Pubs", "dbo", "????"} ?? ??? ???????? ???? ?? ????? ????:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, 
              new Object[] {"Pubs", "dbo", "Employee"});
				
????? ????? ???, ?? ????? ????? ?? ???? ????:
  1. ????? C# ????? ????????? ??? ??? ????????? ?????? ???????? ??? ?? ????????? ?? ??? Class1.cs ????? ??? ???
  2. Class1 ?? ??? ??? ????? ?? ?????? ??? ??? ????? ?? ???? ??? ??? ????? ??? ?????????? ?????declaration:
    using System.Data;
    using System.Data.OleDb;
    					
  3. ??? ????? ??? ????? ??? ?????????????????? ?? ??? ???? ???.:
    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=<username>;
                           Password=<strong 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 ??? ???? ?????? ???????? ????? ????? ??? ???????? ??? ????? ??????
  6. ????? ????????? ??? ???? ?? IDE ?? ???? ???? ?? ??? ENTER ??????

??????

?? ???? ???? ?? ???OleDbSchemaGuid??????? ?? ???, Microsoft .NET ??? ????? ???? ????? Framework ?????????? ????????? ??? (SDK) ?? ???????? ?????? ?????? ?????????? ?? ?? ???? ?? ??? ???? ?? ????? ????OleDbSchemaGuid?????:
OleDbSchemaGuid ?????
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)
???? ??????? ?? ???, ?? ??????? .NET ??? ????? ???? Framework SDK ????????:
?????? ??????? ?? ??????? ?? ??????? ?? ??? ??
HTTP://MSDN.Microsoft.com/en-us/library/kcax58fh.aspx (http://msdn.microsoft.com/en-us/library/kcax58fh.aspx)

OleDbConnection.GetOleDbSchemaTable ????
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 ?????
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 ?????
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 ?????
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)

???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
??????: 
kbhowtomaster kbsystemdata kbmt KB309681 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:309681  (http://support.microsoft.com/kb/309681/en-us/ )