Read and write to an access database using Javascript

前端 未结 2 385
无人及你
无人及你 2020-12-02 17:59

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

相关标签:
2条回答
  • 2020-12-02 18:19

    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.

    0 讨论(0)
  • 2020-12-02 18:35
        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();
    }
    
    0 讨论(0)
提交回复
热议问题