Connecting to Access database using ADO
We will be using ActiveX Data Objects (ADO) to create a connection to Access database. ADO model contains six objects and we will be using Connection String to connect to database.
Creating a Connection String using ADO and opening the connection
To connect to Access database we can create a connection object by using ADO. Here is an example of connection object creation and opening the connection object.
Set objconn = Server.CreateObject("ADODB.Connection")
Note that here we have using server MapPath to locate the access database. Here database is stored inside db directory relative to the script running this code. Now we have opened the connection to the access database next is we need one object to handle records.
objconn.ConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/months.mdb")
To handle records of a MS access table we need recordset object provided by ADO. Using this object we can get a subset of records of our table. We can declare Record set object like this.
Set objRs = Server.CreateObject("ADODB.Recordset")
By just declaring the recordset object we can't handle data, we need to open it and during the opening process we can give different arguments to handle the recordset object. Here is the syntax of opening a Recordset object.
Recordset.Open Source, Connection, cursortype, locktype, commandtype
As you can see above we have used different arguments and let us try to understand each one with some examples.
This can be the table name or sql query or some other types.
Connection which we have established before
This tells how the recodset will move. By default it is forward only.
This tell how we handle the record. As there will be more than one client trying to access / update the data. By default it is read only.
Tells how the source parameter to be evaluated. adCmdTable tells that the source is a table.
Let us try to display all month names stored in an access table.
Be the first to post comment on this article :