Listing Tables in an Access Database
Số trang: 3
Loại file: pdf
Dung lượng: 14.06 KB
Lượt xem: 7
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 10.14 Listing Tables in an Access Database Problem You need a list of all tables in your Access database. Solution Use the GetOLEDBSchemaTable
Nội dung trích xuất từ tài liệu:
Listing Tables in an Access Database [ Team LiB ]Recipe 10.14 Listing Tables in an Access DatabaseProblemYou need a list of all tables in your Access database.SolutionUse the GetOLEDBSchemaTable( ) method of the OleDbConnection class or use ADOXthrough COM interop.The first technique uses the GetOLEDBSchemaTable( ) method to return schemainformation about user tables. These results are then displayed.For the second technique, youll need a reference to the Primary Interop Assembly (PIA)for ADO provided in the file ADODB.DLL; select adodb from the .NET tab in VisualStudio .NETs Add Reference Dialog. Youll also need a reference to Microsoft ADOExt. 2.7 for DDL and Security from the COM tab in Visual Studio .NETs Add ReferenceDialog.The second technique creates an ADOX Catalog object through COM interop. TheTables property of this object accesses the collection of tables from which the name andother information are displayed.The C# code is shown in Example 10-14.Example 10-14. File: ListAccessTablesForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.OleDb;// . . .// OLE DBStringBuilder result = new StringBuilder( );// Open the OLE DB connection.OleDbConnection conn = new OleDbConnection( ConfigurationSettings.AppSettings[MsAccess_ConnectString]);conn.Open( );// Retrieve schema information for all tables.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] {null, null, null, TABLE});result.Append(TABLE + Environment.NewLine);// Iterate over the collection of table records.foreach(DataRow row in schemaTable.Rows){ result.Append(row[TABLE_NAME] + Environment.NewLine);}conn.Close( );resultTextBox.Text = result.ToString( );// ADOXStringBuilder result = new StringBuilder( );// Open the connection.ADODB.Connection conn = new ADODB.ConnectionClass( );conn.Open(ConfigurationSettings.AppSettings[MsAccess_ConnectString], , , 0);// Create an ADOX catalog object for the connecton.ADOX.Catalog cat = new ADOX.Catalog( );cat.ActiveConnection = conn;result.Append(TABLE KEY + Environment.NewLine);// Iterate over the collection of tables.foreach(ADOX.Table table in cat.Tables){ if(table.Type == TABLE) { result.Append(table.Name + Environment.NewLine); // Iterate over the collection of keys for the table. foreach(ADOX.Key key in table.Keys) { result.Append( + key.Name + (); // Iterate over the collection of columns for the key. foreach(ADOX.Column col in key.Columns) { result.Append(col.Name + , ); } result.Remove(result.Length - 2, 2).Append() + Environment.NewLine); } result.Append(Environment.NewLine); }}cat = null;conn.Close( );resultTextBox.Text = result.ToString( );DiscussionThe solution shows two techniques that you can use to get a list of tables in an Accessdatabase.The first technique uses the GetOleDbSchemaTable( ) method of the OLE DB connectionobject. This technique is discussed in Recipe 10.2.The second technique uses ActiveX Database Objects Extensions (ADOX) from COMinterop. ADOX has a Tables property that exposes a collection of Table objects in thedatabase. The user tables are determined by iterating over the collection of tables andselecting only those where Type property of the Table is TABLE.The Table object also exposes collections of Columns, Indexes, Keys, and Properties thatcan be used to further investigate the database. As an example, the sample code iteratesover the collection of Keys in each table to get the list of both primary and foreign keys.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Listing Tables in an Access Database [ Team LiB ]Recipe 10.14 Listing Tables in an Access DatabaseProblemYou need a list of all tables in your Access database.SolutionUse the GetOLEDBSchemaTable( ) method of the OleDbConnection class or use ADOXthrough COM interop.The first technique uses the GetOLEDBSchemaTable( ) method to return schemainformation about user tables. These results are then displayed.For the second technique, youll need a reference to the Primary Interop Assembly (PIA)for ADO provided in the file ADODB.DLL; select adodb from the .NET tab in VisualStudio .NETs Add Reference Dialog. Youll also need a reference to Microsoft ADOExt. 2.7 for DDL and Security from the COM tab in Visual Studio .NETs Add ReferenceDialog.The second technique creates an ADOX Catalog object through COM interop. TheTables property of this object accesses the collection of tables from which the name andother information are displayed.The C# code is shown in Example 10-14.Example 10-14. File: ListAccessTablesForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.OleDb;// . . .// OLE DBStringBuilder result = new StringBuilder( );// Open the OLE DB connection.OleDbConnection conn = new OleDbConnection( ConfigurationSettings.AppSettings[MsAccess_ConnectString]);conn.Open( );// Retrieve schema information for all tables.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] {null, null, null, TABLE});result.Append(TABLE + Environment.NewLine);// Iterate over the collection of table records.foreach(DataRow row in schemaTable.Rows){ result.Append(row[TABLE_NAME] + Environment.NewLine);}conn.Close( );resultTextBox.Text = result.ToString( );// ADOXStringBuilder result = new StringBuilder( );// Open the connection.ADODB.Connection conn = new ADODB.ConnectionClass( );conn.Open(ConfigurationSettings.AppSettings[MsAccess_ConnectString], , , 0);// Create an ADOX catalog object for the connecton.ADOX.Catalog cat = new ADOX.Catalog( );cat.ActiveConnection = conn;result.Append(TABLE KEY + Environment.NewLine);// Iterate over the collection of tables.foreach(ADOX.Table table in cat.Tables){ if(table.Type == TABLE) { result.Append(table.Name + Environment.NewLine); // Iterate over the collection of keys for the table. foreach(ADOX.Key key in table.Keys) { result.Append( + key.Name + (); // Iterate over the collection of columns for the key. foreach(ADOX.Column col in key.Columns) { result.Append(col.Name + , ); } result.Remove(result.Length - 2, 2).Append() + Environment.NewLine); } result.Append(Environment.NewLine); }}cat = null;conn.Close( );resultTextBox.Text = result.ToString( );DiscussionThe solution shows two techniques that you can use to get a list of tables in an Accessdatabase.The first technique uses the GetOleDbSchemaTable( ) method of the OLE DB connectionobject. This technique is discussed in Recipe 10.2.The second technique uses ActiveX Database Objects Extensions (ADOX) from COMinterop. ADOX has a Tables property that exposes a collection of Table objects in thedatabase. The user tables are determined by iterating over the collection of tables andselecting only those where Type property of the Table is TABLE.The Table object also exposes collections of Columns, Indexes, Keys, and Properties thatcan be used to further investigate the database. As an example, the sample code iteratesover the collection of Keys in each table to get the list of both primary and foreign keys.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Listing Tables in an Access DatabaseTài liệu liên quan:
-
52 trang 434 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 293 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0