Read and write to an access database using Javascr

2020-01-29 04:09发布

问题:

first I want to mention that I am aware that in web interaction with databases should
always be with server side languages due to security reasons and for the fact that javascript
as is doesn't offer and compatibility with the windows file system.

that said I am facing a though situation and I am trying to think creatively.
I am not allowed any access to a server sided scripting and SQL.

and I need to create a client based application for an intranet that will be able to store data as time progress.

I have found 2 solutions so far but none of them has enough documentation for me to use correctly.

one is a javascript library called ACCESSdb which can be found here:ACCESSdb
unfortunately I couldn't understand how to use it to write or read data from the DB...

and the other is those 3 pieces of code:

Adding a Record:

function AddRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='/\dbName.mdb'");
adoRS.Open("Select * From tblName", adoConn, 1, 3);

adoRS.AddNew;
adoRS.Fields("FieldName").value = "Quentin";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}  

Removing a Record:

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);
adoRS.Delete;
adoRS.Delete;

adoRS.Close();
adoConn.Close();
}  

Editing a Record:

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);

adoRS.Edit;
adoRS.Fields("FieldName").value = "New Name";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}  

out of them only the add new record one worked for me for some reason...
also I found that to read the value of any cell in the first row all I had to do was to write:

alert(adoRS(cellNum));  

but how do I get the value of cells in the later rows? lets say (row 3,cell 5).

Thank you for reading this far! I'll appreciate your help a lot!

Jake

回答1:

First, make sure that '/\' and '\' (in connection string) is just a typo in SO.

Second, here is a version of Delete command:

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoCmd = new ActiveXObject("ADODB.Command");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText = "Delete * From tblName Where FieldName = 'Quentin'";
adoCmd.Execute();

adoConn.Close();
}

And, Edit command (without looping -> updates all [matching] records):

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoCmd = new ActiveXObject("ADODB.Command");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText = "Update tblName Set FieldName = 'New Value' Where FieldName = 'Quentin'";
adoCmd.Execute();

adoConn.Close();
}  

Please note, I have not tested this (do not have Access right now), so there might be some syntax bugs...

Hope it works and helps.



回答2:

    function loadDB() {

    var connection = new ActiveXObject("ADODB.Connection");
    var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=Micr0s0ft;Provider=SQLOLEDB";
    connection.Open(connectionstring);

    var rs = new ActiveXObject("ADODB.Recordset");
    rs.Open("select * from emp", connection);
    rs.MoveFirst();

    var span = document.createElement("span");

    span.style.color = "Blue";

    span.innerText = "  ID " + "  Name " + "   Salary";

    document.body.appendChild(span);

    while (!rs.eof){

        var span = document.createElement("span");
        span.style.color = "green";

        span.innerText = "\n " + rs.fields(0) + " |  " + rs.fields(1) + " |  " + rs.fields(2);

        document.body.appendChild(span);

        rs.MoveNext();
    }
    rs.close();
    connection.close();
}