Suppose you need to open a database and read from its tables to display the data on a web form. You should follow these steps:
1- Make a script in HTML page to write the code to access database using ActiveX controls. For example you can write it in vbscript or JavaScript.
2- Inside the script create a method to declare and initialize activeX object
3- Create ActiveXObject for a connection
4- Define the connection string and open the connection object with this connection string
5- Create ActiveXObject for a recordset
6- Define a query and open the recordset
7- Read or write in the recordset
8- Close the recordset
9- Close the connection
These steps should be included in a function inside the script like this:
<script language = "javascript" type = "text/javascript">
function getEmployess(){
var cn = new ActiveXObject("ADODB.Connection");
………
………
}
</script>
let's illustrate these steps by the following example:
Suppose there is an access database have information about company. One table is Employee table that we want to read from. Each employee has a unique id and other information like name , salary and date of birth.
To do this we should go through these steps :
1- Create an ActiveX object for a connection:
Any ActiveXObject is created like this:
newObj = new ActiveXObject(servername.typename[, location])
Arguments:
newObj : Required. The variable name to which the ActiveXObject is assigned.
servername : Required. The name of the application providing the object.
typename : Required. The type or class of the object to create.
location : Optional. The name of the network server where the object is to be created.
In our example:
// create a connection
var cn = new ActiveXObject("ADODB.Connection");
2- Declare connection string
Set the connection string to the data source. You should specify the data source and any other parameters you need. Make sure to use the correct provider in the connection string.
//create a connection string
var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\\myDataBase.accdb";
3- Open the connection object
To open the connection object previously created you should use the connection string as a parameter.
// open the connection
cn.Open(strConn);
4- Create an ActiveX object for a recordset
As in step 1, create a recordset.
// create a recordset
var rs = new ActiveXObject("ADODB.Recordset");
5- Populate the recordset (Define query and open the recordset)
Write your SQL query needed to access the database and then use the query string and the connction object as parameters to open the recordset.
var SQL = "select Employee_Name, Employee_Salary, Employee_Birthdate from Employee";
rs.Open(SQL, cn);
6- Manipulate the recordset
You can read or write to the record set. There are multiple methods and properties you can use recordset like:
· "MoveFirst": Method to move to the first record.
· "MoveLast": Method to move to the last record.
· "MoveNext": Method to move to the next record.
· "MovePrevious": Method to move to the previous record.
· "BOF": Property to return true if the current record pointer is before the first record.
· "EOF": Property to return true if the current record pointer is after the last record.
· "Fields": Property to return a collection representing all the fields of the current record.
You can also read the fields in the current record in the record set by specifying the index of the field in the recordset object. For example: rs(0) returns the value of first field in the current record in the recordset rs.
7- Close the recordset
// close the recordset object
rs.Close();
8- Close the connection
// close the recordset object
cn.Close();