html - vbscript在html表中显示数据库查询

标签 html mysql sql vbscript

我见过几个例子,但都是用 ASP 或 PHP 编写的。我工作的公司有很多安全措施,我不允许在我的计算机上安装其中任何一个进行开发,因此,我需要在 HTML 中执行此操作。

我创建了一个表单,用于收集用户信息并通过 VBScript 将其提交到 2010 Access 数据库。我需要弄清楚如何在 HTML 表格中显示它。

这是在数据库中查询表中所有内容的 VBScript 子程序:

Sub SelectFromDatabase()

    Dim strSQL, strConnect

    Dim conn, rs

    Set conn = CreateObject("ADODB.Connection")

    strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\My Stuff\MyData.accdb;Persist Security Info=False;"

    conn.Open strConnect

    strSQL = "SELECT * FROM SurveyData"

    ' Send the query to the database and save result
    Set rs = conn.Execute strSQL

    ' Close the connection to the database
    conn.Close
    strSQL = ""

end sub

我创建了一个单独的子程序,它调用前一个子程序并将数据存储在变量中:

sub PrintData
    call SelectFromDatabase()
    strFullName = FullName
    strAddress = Address
    strCity = City
    strState = State
    strZip = Zip
    strPhone = Phone
    strEmail = Email
    strComputer = Computer
    strOS = OS
    strShopping = Shopping
end sub

这是我用来将数据打印到 HTML 表的 HTML:

<html>
    <head>
        <title>My Web Site - Customer Data</title>
        <script language="vbscript" type="text/vbscript" src="Process.vbs">
        </script>
    </head>
    <body onLoad="SelectFromDatabase();">
        <h1 align=center>My Web Site</h1>
        <h2>Customer Data</h2>
        <p>Below is a list of the customer data that has been submitted.</p>
        <hr>
        <script language="vbscript" type="text/vbscript">
            document.writeln("<table border=1><tr><th>Name</th><th>Address</th><th>City</th><th>State</th><th>Zip</th><th>Phone</th><th>Email</th><th>Computer</th><th>OS</th><th>Shopping</th></tr>")
            Do Until rs.EOF
                document.writeln("<tr><td>" & rs(0) & "</td><td>" & rs(1) & "</td><td>" & rs(2) & "</td><td>" & rs(3) & "</td><td>" & rs(4) & "</td><td>" & rs(5) & "</td><td>" & rs(6) & "</td><td>" & rs(7) & "</td><td>" & rs(8) & "</td><td>" & rs(9) & "</td></tr>")
                rs.MoveNext
            Loop
            document.writeln("</table>")
        </script>
    </body>
</html>

我收到的错误出现在 for every... 行中。据说 PrintData 不是一个集合,我同意这一点,但我现在不确定我是否会调用子 PrintData 并能够在HTML 表格。

最佳答案

请参阅我上面关于将其切换到 HTA 的评论。完成此操作后,您可以创建 ActiveX 控件(Connection 对象、Recordset 对象等)来查询数据库,而不会引起 Internet Explorer 的提示。

您的SelectFromDatabase函数不会保存生成的记录集。您正在执行查询,这很棒,但您没有保存结果集。 Connection.Execute() 返回一个 Recordset 对象。

' in SelectFromDatabase()...
strSQL = "SELECT * FROM SurveyData"

' Send the query to the database AND save the result!
Set rs = conn.Execute(strSQL)

现在rs包含从您的查询返回的记录。 rs(0) 是第一列的值,rs(1) 是第二列的值,依此类推。您可以像这样循环访问这些记录:

Do Until rs.EOF
    ' Write to page...
    document.writeln("<tr><td>" & rs(0) & "</td><td>" & rs(1) & "</td></tr>")
    rs.MoveNext
Loop

作为循环的替代方法,您还可以使用 Recordset 类的 GetString() 函数将所有记录格式化为表:

document.write("<table><tr><td>" & rs.GetString(, , "</td><td>","</td></tr><tr><td>") & "</td></tr></table>")

这是一个完整的工作示例:

<html>
<body>
<script language="vbscript">
    Sub SelectFromDatabase()

        Dim cn, rs, e

        Set cn = CreateObject("ADODB.Connection")
        cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\My Stuff\MyData.accdb;Persist Security Info=False;"

        Set rs = cn.Execute("SELECT * FROM SurveyData")

        Set e = document.getElementById("content")
        e.innerHTML = "<table><tr><td>" & rs.GetString(2, , "</td><td>", "</td></tr><tr><td>") & "</td></tr></table>"

        rs.Close
        cn.Close

    End Sub
</script>
<body onload="SelectFromDatabase()">
    <div id="content"></div>
</body>
</html>

关于html - vbscript在html表中显示数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23139852/

相关文章:

jquery - 为什么 .html() 不能在类 ="error aTitle"上工作

javascript - 样式输入类型 ="file"- 文件名 javascript-solution 不起作用

基于最高日期和非唯一列的 MySQL JOIN

sql - 选择计数/重复

sql - 在postgresql中安排选择查询?

mysql - 如何返回 2 COUNT()

javascript - 多行输出文本

html - 如何以类似网格的方式定位我的内容

php - mySQL - 不要将相同的数据插入表中的行(如果存在)

php - 处理数据库查询的最佳方法是什么