使用Javascript动态创建表以显示数据库SELECT查询输出

时间:2016-01-02 19:29:30

标签: javascript html search sql-insert

我有以下代码:

<!doctype html>
<html>
<title>Search</title>
<script type="text/javascript">

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 + ", ");
        document.write(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 + ".</p>");
        var row = row.parentNode.rows[ ++idx ];
            document.write(rs.fields(1).value + ".</p>");
            rs.MoveNext();
        }
    }
    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>

请帮我修改代码,以便在单击“搜索”按钮时,必须创建一个包含16列且具有相应列标题的新表,并且搜索结果应正确插入表行中。

1 个答案:

答案 0 :(得分:1)

这是一种完成的方法,用HTML表格构建一个字符串...
另外,我循环fields集合以获取列名和值。

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

    var table = '';
    var tableRow = '';
    var headings = true;

    if (rs.EOF) {
        document.write("<p>No data found.</p>");
    } else {
        while (!rs.EOF) {
          if (headings) {
            tableRow = '';
            for (var i = 0; i < rs.fields.count; i++) {
              switch (i + 1) {
                case 1:
                case 3:
                case 4:
                case 6:
                case 8:
                case 9:
                  tableRow = tableRow + '<td>' + rs.fields(i).name + '</td>';
                  break;
              }
            }
            tableRow = '<tr>' + tableRow + '</tr>';
            table = table + tableRow;
            headings = false;
          }
          tableRow = '';
          for (var i = 0; i < rs.fields.count; i++) {
              switch (i + 1) {
                case 1:
                case 3:
                case 4:
                case 6:
                case 8:
                case 9:
                  tableRow = tableRow + '<td>' + rs.fields(i).value + '</td>';
                  break;
              }
          }
          tableRow = '<tr>' + tableRow + '</tr>';
          table = table + tableRow;
          rs.MoveNext();
        }
        document.write('<table>' + table + '</table>');
    }
    rs.Close();
    cn.Close();
}