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
  %> 

You might also like...

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

0 Comments

Recent Comments

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...

Mau 06/06/2017 08:58
In response to Razor Pages - The Elevator Pitch
I am missing the razor view code. Thank you for your article. Design pattern should improve the of...

Obinna Okafor 26/05/2017 16:16
In response to Razor Pages - The Elevator Pitch
Thank you for this wonderful piece, Mike. I need more of these...Keep them coming - anything more...

Cyrus 26/05/2017 06:00
In response to Razor Pages - The Elevator Pitch
There are some concern about razor pages performance. Is it faster or slower than MVC? would you a a...

Cyrus 26/05/2017 05:44
In response to Razor Pages - Understanding Handler Methods
well done, thank you....

Satyabrata Mohapatra 23/05/2017 11:41
In response to Razor Pages - Understanding Handler Methods
Nice and easy !! Great post....

Cyrus 16/05/2017 19:55
In response to Razor Pages - Getting Started With The Preview
There is something wrong related to microsoft.dotnetcore.mvc.taghelpers! if you remove it from page...

Cyrus 16/05/2017 10:18
In response to Razor Pages - Getting Started With The Preview
well done mike, it was very useful, I really appreciate that....

Satyabrata Mohapatra 16/05/2017 07:21
In response to Razor Pages - Getting Started With The Preview
Finally!!!! web pages in asp.net core!!! Super excited !!!! Thank u sir for sharing.....Awaiting on...

Daniele 14/03/2017 10:24
In response to Working With Zip Files In ASP.NET MVC
is it possible give to the user a progress bar of the zipping process? Thanks in advance. ...