JavaScript函数,使用具有多个参数的Recordsets查询MS Access DB

时间:2016-03-12 11:14:49

标签: javascript sql database select adodb

我有一个查询MS Access数据库的前端HTML页面。

HTML:

<input class="textbox" id="searchValue" maxlength="100" name="KeywordSearch" onclick="this.value='';" size="50" type="text" value="Enter Your Keyword Here" />

<input class="textbox" id="ForCLNo" name="CLNum"  type="text" onclick="this.value='';" size="25" type="text" value="CL Number"/> 

<input class="button" name="Search" onclick="searchEngineSearch();" type="button" value="Search" /></p>

想要执行此查询:

SELECT * FROM MasterTable where CLNo = test1 AND Query = test2;

对于查询SELECT * FROM MasterTable where Query LIKE test2,我创建了ADODB对象,如下所示:

var adVarWChar = 202;
var adParamInput = 1;
var pad = "C:\\Users\\Rik\\Desktop\\Project\\MyTable.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 MasterTable where Query LIKE test2";

cm.Parameters.Append(cm.CreateParameter(
        "test2",
        adVarWChar,
        adParamInput,
        255,
        "%" + document.getElementById("searchValue").value + "%"));
var rs = cm.Execute();

并获得正确的结果。

不确定如何重写cm.Parameters.Append(cm.CreateParameter())函数以在SQL查询[SELECT * FROM MasterTable where CLNo = test1 AND Query = test2;]中合并多个WHERE条件。

请帮助:)

1 个答案:

答案 0 :(得分:0)

您已经在Parameters集合中创建了一个参数......您只需再创建一个参数:

cm.CommandText = "SELECT * FROM MasterTable where Query LIKE test1 OR Query LIKE Test2";

cm.Parameters.Append(cm.CreateParameter(
    "test1",
    adVarWChar,
    adParamInput,
    255,
    "%" + document.getElementById("searchValue").value + "%"));


cm.Parameters.Append(cm.CreateParameter(
    "test2",
    adVarWChar,
    adParamInput,
    255,
    "%" + document.getElementById("searchValue").value + "%"));

var rs = cm.Execute();

cm.Parameters是一个集合,这意味着它能够保存任何合理数量的项目类型。对于上面的每个参数代码,您将新创建的参数附加到集合中。然后使用整个集合来解析SQL语句。