How to search an MS Access Database Table using Javascript?

こ雲淡風輕ζ 提交于 2019-12-10 12:06:11

问题


I have an MS Access Database Table 'ImportFromExcel' having 16 columns. When 'Search' key in the webpage is pressed after entering keyword(s), it should go to table and check if the keyword(s) is/are present anywhere in the table. If the keyword is present in a row, that row should be printed. Else, it should throw a message.

Code:

<!doctype html>
<html>
<title>Search</title>

<script type="text/javascript">

function query()
{
    var pad = "C:\\Users\\azi!z\\Desktop\\Project\\Test.accdb";
    var cn = new ActiveXObject("ADODB.Connection");
    var strConn = "Provider=microsoft.ace.oledb.12.0;Data Source=" + pad;
    cn.Open(strConn);
    var rs = new ActiveXObject("ADODB.Recordset");
    var SQL = “SELECT * FROM ImportFromExcel where TestCase ALIKE '%searchTerm%'”;
    rs.Open(SQL, cn);
    if(!rs.bof) 
    {
        rs.MoveFirst();
        if(!rs.eof)
        {
            document.write("<p>" + rs.fields(1).value + ", ");
            document.write(rs.fields(2).value + ", ");
            document.write(rs.fields(3).value + ", ");
            document.write(rs.fields(4).value + ", ");
            document.write(rs.fields(5).value + ", ");
            document.write(rs.fields(6).value + ", ");
            document.write(rs.fields(7).value + ", ");
            document.write(rs.fields(8).value + ", ");
            document.write(rs.fields(9).value + ", ");
            document.write(rs.fields(10).value + ", ");
            document.write(rs.fields(11).value + ", ");
            document.write(rs.fields(12).value + ", ");
            document.write(rs.fields(13).value + ", ");
            document.write(rs.fields(14).value + ", ");
            document.write(rs.fields(15).value + ", ");
            document.write(rs.fields(16).value + ".</p>");
        }
    }
    else
    {
        document.write("No data found");
    }
    rs.Close();
    cn.Close();
}

</script>
</head>

<body>
<form method="get" name="SearchEngine" target="_blank">
<p style="text-align: center;"><span style="font-family:times new roman,times,serif;"><span style="font-size: 36px;"><strong>EA Search Engine</strong></span></span></p>

<p style="text-align: center;">&nbsp;</p>

<p style="text-align: center;"><input maxlength="300" id="searchTerm" name="KeywordSearch" size="100" type="text" value="Enter Your Keyword Here" /></p>

<p style="text-align: center;">&nbsp;</p>

<p style="text-align: center;"><input name="Search" type="button" value="Search" onclick="query();" /></p>
</form>
</body>
</html>

Tried the query in MS Access and it's giving the expected result (Thanks to @HansUp). Seems to be a problem with the JavaScript piece. Please help :)


回答1:


So now that you've gotten the JavaScript function to actually execute, you need to make the following changes:

  1. Hard-coding '%searchTerm%' into the script will only search for that literal text. You want to incorporate the value of the "searchTerm" <input> box on the form.

  2. In order to do that you need to use a parameterized query.

  3. You also want to loop through the Recordset to retrieve all of the rows returned, instead of just the first one.

So, your JavaScript code should look more like this:

function query() {
    var adVarWChar = 202;
    var adParamInput = 1;

    var pad = "C:\\Users\\azi!z\\Desktop\\Project\\Test.accdb";
    var cn = new ActiveXObject("ADODB.Connection");
    var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pad;
    cn.Open(strConn);
    var cm = new ActiveXObject("ADODB.Command");
    cm.ActiveConnection = cn;
    cm.CommandText = "SELECT * FROM ImportFromExcel where TestCase LIKE ?";
    cm.Parameters.Append(cm.CreateParameter(
            "?", 
            adVarWChar, 
            adParamInput, 
            255, 
            "%" + document.getElementById("searchTerm").value + "%"));
    var rs = cm.Execute();  // returns ADODB.Recordset object
    if (rs.EOF) {
        document.write("<p>No data found.</p>");
    } else {
        while (!rs.EOF) {
            document.write("<p>" + rs.fields(0).value + ", ");
            // (list of fields truncated for clarity)
            document.write(rs.fields(1).value + ".</p>");
            rs.MoveNext();
        }
    }
    rs.Close();
    cn.Close();
}


来源:https://stackoverflow.com/questions/34481025/how-to-search-an-ms-access-database-table-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!