ADOX, MS Access and ASP

4.5 (2 votes)

You don't need MS Access installed on your computer to create a database file for your ASP web site. Using ADOX and some data definition language allows you to produce an mdb file programmatically.

ADO (ActiveX Data Objects) are used primarily by ASP programmers for manipulating data - that is, retrieving it from data stores to display on web pages, and allowing users to insert or modify data in data stores.

ADO can also be used as a method of passing DDL (Data Defintion Language) commands to create, drop and alter tables and their fields within databases, but what about creating a new Access database, for example, or other schema based activities?

ADOX (ADO Extensions) is the tool for this. 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. Here's how to use it to create a new Access .mdb file and add a table with a primary key.

 
<%
  'Const adVarWChar = 202
  'Const adInteger = 3
  'The above are from adovbs.inc and are the ADO Constants 
  'for the two datatypes used in this example.
  
  Dim objADOXDatabase
  'Instantiate a Catalog object
  Set objADOXDatabase = CreateObject("ADOX.Catalog")
  
  'Use the Create method, passing the Datasource Provider 
  'and full path and file name as parameters
  objADOXDatabase.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=PathToDatabasecustomers.mdb"
  'The database now exists...
  
  'Create a database table and name it
  Dim objFirstTable 
  Set objFirstTable = CreateObject("ADOX.Table")
  objFirstTable.Name = "Customers"
  
  'CustomerID will be of datatype adInteger, for which the ADO Constant is 3
  objFirstTable.Columns.Append "CustomerID", 3
  
  'CustomerName is of datatype adVarWChar, for which the ADO Constant is 202. 
  'The field size is 30 characters
  objFirstTable.Columns.Append "CustomerName", 202, 30
  
  'Create a key, passing first the name of the key, 
  'then the type (1 = primary key), 
  'then the column on which it is created
  objFirstTable.Keys.Append "PK_CustomerID", 1, "CustomerID"
  
  'Add the database table to the database 
  objADOXDatabase.Tables.Append objFirstTable
  response.write "Database created"
  'Clean up...
  Set objFirstTable = Nothing
  Set objADOXDatabase = Nothing
  %> 

Date Posted:
Last Updated:
Posted by:
Total Views to date: 9265

0 Comments

Recent Comments

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....