LogoASPAlliance: Articles, reviews, and samples for .NET Developers
CodeSnip: Access Database Schema with ASP.NET 2.0
by Andrew Mooney
Average Rating: 
Views (Total / Last 10 Days): 15747/ 39

[ Run Sample ]
Retrieving Access database schema with ASP.NET 2.0 is actually very simple. First you create an OleDbConnection to a database. Next, you fill a DataTable with schema information using GetSchema method. Finally, you bind the DataTable to a GridView.

When you use the GetSchema method of the OleDbConnection with no overloads it returns the same information that you get when requesting MetaDataCollections.

// These two line get you the same information.

Using either of the above methods gives you a list of what schema information you can get from the database. This is a complete list from an Access 2003 database:

  • MetaDataCollections
  • DataSourceInformation
  • DataTypes
  • Restrictions
  • ReservedWords
  • Columns
  • Indexes
  • Procedures
  • Tables
  • Views

You can also use restrictions with the GetSchema method to get more specific information. For example, you might want a list of columns for just one table. Or, you may want a list of tables that does not include hidden system tables. Restrictions are a string array in this format: {TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE}.

// Get all of the column information from Table1.
string[] restrictions1 = new string[] { null,null, "Table1",null };
System.Data.DataTable DataTable1 = OleDbConnection1.GetSchema("Columns", restrictions1);

// Get all of the table information except system tables.
string[] restrictions1 = new string[] { null, null, null, "TABLE" };
System.Data.DataTable DataTable1 = OleDbConnection1.GetSchema("Tables", restrictions1);

The AutoNumber data type shows up in the schema as a long integer. This means that there is no way to determine if a field auto increments. I have found a work around for this. I type "AutoNumber" in the description for AutoNumber fields and this can be seen in the sample. Since, description is the last field you may need to scroll to the right.

Sample web page for retrieving Access database schema:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
<script runat="server">
private void Page_Load(object sender, EventArgs e)
System.Data.OleDb.OleDbConnection OleDbConnection1 = 
new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
System.Data.DataTable DataTable1 = OleDbConnection1.GetSchema();
GridView1.DataSource = DataTable1;
<html xmlns="" >
<head runat="server">
<title>Database Schema</title>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" Runat="server">

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-05 6:45:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search