When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, January 31, 2001

Working with ADOX
By Ramesh Balaji and Scott Mitchell


Introduction
ADO, as you likely know, is used to access databases (or more generic data stores). The work that we normally perform with ADO when working with database can be categorized into the following two activities:

    - continued -

    1. Data Definition
    2. Data Manipulation

ADO is primarily used for Data Manipulation like Inserting, Updating, Deleting and Querying data. As far as defining data, like Creating Tables, Indexes, Users, Groups we normally create SQL Statements and run those queries using ADO Connection and Command Objects. (For an example see: How do you create a database table through a SQL statement (via an ASP page, perhaps)?) There are no objects available in ADO to perform these schema related activities like Table creation or User creation etc.

With the release of ADO 2.1, Microsoft provided a new set of ADO objects to perform schema related activities: ADOX, which stands for ADO Extensions. ADOX is an object model that provides a set of objects that can be used perform activities pertaining to Data Definition. If you do not have ADO 2.1 or later, you can download the latest version of ADO (for free) from: http://www.microsoft.com/data/download.htm. Simply download the latest MDAC SDK.

For the rest of this article we will be examining some examples of using ADOX to create an Access database, a database table, and a query. Before you delve into the examples you may wish to familiarize yourself with the ADOX object model. The following links are great places to start:

The ADOX object model starts with the Catalog object, which is the top-level object. The Catalog object contains a number of objects as properties, each of which are collections. (For example, the Tables property of the Catalog object is a collection of Table objects.) These Catalog object properties include:

  • Tables
  • Groups
  • Users
  • Procedures
  • Views

The Catalog object represents the top level object, schema information about a data store. In this article we will be examining using ADOX with an Access database (.MDB file).

Creating a Catalog
For our first example, let's create a new Access database. We can do this via the Create method of the Catalog object. This will, literally, create an Access database, generating a .MDB file. We will start by declaring instantiating a Catalog object.

Dim objADOXDatabase
Set objADOXDatabase = Server.CreateObject("ADOX.Catalog")

Next we'll use the Create method of the Catalog object to specify the location of the Database and the appropriate OLEDB provider of the Datasource as the parameters .

objADOXDatabase.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\customer.mdb"

The Catalog is now successfully created. This will create an actual Access database file, customer.mdb in the C:\ directory. You can, of course, change the name and directory location by modifying the Data Source parameter in the OLE DB connection string. Also, note that if a database with the name you specify already exists you will receive the following error: Microsoft JET Database Engine error '80040e17': Database already exists.

Creating a Table and Specifying a Primary Key At this point we have created the Access database file, so let us go ahead and create a database table. This is done using the Table object.

Dim objFirstTable 
Set objFirstTable = Server.CreateObject("ADOX.Table")
objFirstTable.Name = "NewADOX"
objFirstTable.Columns.Append "CustID", adInteger
objFirstTable.Columns.Append "CustName", adVarWChar, 30
objFirstTable.Keys.Append "PK_CustID", 1, "CustID"

The above code starts by creating an instance of the Table object. Next, we give the table a name (NewADOX) and add two columns to it. The first column, CustID is of type integer. The next column, CustName is a 30 character long wide-varchar. (When using Access 2000, you will need to use the unicode adWChar and adVarWChar as opposed to the adChar and adVarChar data types.) These constants, adInteger and adVarWChar, are defined in adovbs.inc, which will need to be included in your ASP pages either through a server-side include or through the METADATA tag. For more information on adovbs.inc be sure to read: adovbs.inc - Use It!.

Next, we create a primary key constraint on the CustID column. This is done via the Keys object's Append method. The first parameter is the name of the key, the second parameter specifies what type of key (1 == Primary Key) and the third parameter specifies what column to place the key on (in this case, CustID).

After specifying all the parameters necessary for the table, we still need to add the table to our database, customer.mdb. We do this via the Append method of the Tables property of the Catalog object:

objADOXDatabase.Tables.Append objFirstTable

NewADOX table diagram. Pretty neat, eh? We've programmatically created an Access database and appended to it a database table with two columns and a primary key constraint! All using ADOX's object-based model instead of cryptic SQL commands. The figure to the right illustrates the NewADOX table structure when viewed through Access 2000. Remember, this table was created programmatically by our ASP script using ADOX, not through the Access interface.

Now that we've examined how to create an Access database and database table, let's look at how we can iterate through the tables of a database using ADOX. We'll look at this in Part 2 of this article. Before we do, though, take a moment to examine the complete source code of the ASP page we dissected earlier. (Recall that this script created both an Access database file and a table (NewADOX with two columns...)

  • Read Part 2!


    Source Code
    <% Option Explicit %>
    <!--#include file="adovbs.inc"-->
    <%
    Dim objADOXDatabase
    Set objADOXDatabase = Server.CreateObject("ADOX.Catalog")
    
    'Create the database file
    objADOXDatabase.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                           "Data Source=c:\customer.mdb"
    
    'Create a database table and add columns/PK
    Dim objFirstTable 
    Set objFirstTable = Server.CreateObject("ADOX.Table")
    objFirstTable.Name = "NewADOX"
    objFirstTable.Columns.Append "CustID", adInteger
    objFirstTable.Columns.Append "CustName", adVarWChar, 30
    objFirstTable.Keys.Append "PK_CustID", 1, "CustID"
    
    'Add the database table to the database catalog
    objADOXDatabase.Tables.Append objFirstTable
    
    'Clean up...
    Set objFirstTable = Nothing
    Set objADOXDatabase = Nothing
    %>
    


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES