VBS下拉列表 - 从sql server中提取的选项

时间:2016-06-23 16:51:59

标签: sql-server vbscript asp-classic

我正在努力修复此VBScript,以允许我使用下拉列表中的数据作为我的SQL查询的变量。

这是我的default.asp页面。在这里,用户选择他们想要查询的客户和日期。我的VBS最初设计用于从数据库中选择客户名称,提交编号和城市。现在我关心的是customerName列:

<!DOCTYPE html>
<html>
<head><title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

    <script>
        $(function() {
        $( "#datepickstart" ).datepicker();
    });
    </script>

    <script>
        $(function() {
        $( "#datepickend" ).datepicker();
    });
    </script>

    <script>
        $(function(){
        $("#header").load("/../header.htm");
    });
    </script>
    <script language="JavaScript">
    function selSummary_OnChange(stype,customer) {
        document.frmSummary.customer.Value = customer;
        document.frmSummary.Summary.Value = stype;
        document.frmSummary.action = "default.asp";
        document.frmSummary.submit();
    }
    </script>
</head>

<body>

    <div id="header"></div>

<form action="billing.asp" id="billing" method="post" target="_blank">
    <table>
        <tr><th colspan=4><b>TESTING!</b></th></tr>
        <tr>
            <td>Customer:</td>
            <td><select name="customer">
                <% if Request.Form("customer") = "0,0,0" then %>
                    <option value="0" selected>All
                <% else %>
                    <option value="0">All
                <% end if %>
                <%
                        dim DBCONN
                set DBCONN = Server.CreateObject("ADODB.Connection")
                DBCONN.CommandTimeout = 60000
                DBCONN.ConnectionTimeout = 60000
                DBCONN.Open "DSN=***;UID=***;PWD=***"
                set DBConnection = DBCONN
                strSQL = "select customerName from log.dbo.customer order by customerName"
                'set DBConnection = GetDataConnection 
                set DBQuery = Server.CreateObject("ADODB.Command")
                DBQuery.ActiveConnection = DBConnection
                DBQuery.CommandType = 1
                DBQuery.CommandText = strSQL
                set dbCS = DBQuery.Execute

                dbCS.MoveFirst  
                while not dbCS.EOF
                    strcustomerIdentifier = dbCS.Fields("customerNo").Value & "," & dbCS.Fields("SubNo").Value & "," & dbCS.Fields("city")
                    Response.Write("<option value='" & strcustomerIdentifier & "'")
                    if Request.Form("customer") = strcustomerIdentifier then
                        Response.Write("selected")
                    end if
                    Response.Write(">" & dbCS.Fields("customerName").Value & chr(13))
                    dbCS.MoveNext
                wend
                Set dbCS = Nothing
                %>
                </select></td>
            <td>Start Date: <input type="text" name="datepickstart" id="datepickstart"></td>
            <td>End Date: <input type="text" name="datepickend" id="datepickend"></td>
        </tr>
        <tr>
            <td><input type="submit" value="Submit" id="billingsubmit"></td>
        </tr>
    </table>
    </form>

</body>
</html>

从那里我们去billing.asp。该页面应根据用户以前的输入吐出HTML表格。我希望能够将客户表中的选择用作我的SQL字符串中的变量,但我不确定如何正确地完成该操作,因为我的尝试没有起作用。

我知道&#34;所有&#34;在我的SQL查询中不会成为一个成功的变量,所以我试图调用2个不同的查询。它们之间的唯一区别是添加(和#34;&#34; +#+ +#34;&#39;)来指定单个客户。

billing.asp

<%@ Language=VBScript %>
<html>
<head><title></title>
</head>
<body>
<%
dim startdate
    startdate = request.form("datepickstart")
dim enddate
    enddate = request.form("datepickend")
dim customer
    customer = request.form("customer")
%>

<%
    dim dbconn
    set dbconn = server.createobject("adodb.connection")
    DBCONN.CommandTimeout = 60000
    DBCONN.ConnectionTimeout = 60000
    dbconn.open "dsn=***;uid=***;pwd=***"

    dim billingtrans
    dim sqlstr
if request.form("customer")="All"
then
    sqlstr = "SELECT sq.*, sq.Total - sq.[Update] as Inquiry from ( select f.customerName, t.[city], sum (t.TransactionCount) as Total, sum (case when  ([format] in (23,25,38) or [format] between 400 and 499 or [format] between 800 and 899) then t.TransactionCount else 0 end) as [Update] FROM [log].[dbo].[TransactionSummary] t INNER JOIN [log].[dbo].[customer] f on t.customerNo = f.customerNo and t.city = f.city and t.subno = f.subno where t.transactiondate between '" + startdate + "' and '" + enddate + "' group by f.customerName,t.city ) sq"
else
    sqlstr = "SELECT sq.*, sq.Total - sq.[Update] as Inquiry from ( select f.customerName, t.[city], sum (t.TransactionCount) as Total, sum (case when  ([format] in (23,25,38) or [format] between 400 and 499 or [format] between 800 and 899) then t.TransactionCount else 0 end) as [Update] FROM [log].[dbo].[TransactionSummary] t INNER JOIN [log].[dbo].[customer] f on t.customerNo = f.customerNo and t.city = f.city and t.subno = f.subno where t.transactiondate between '" + startdate + "' and '" + enddate + "' and '" + customer + "' group by f.customerName,t.city ) sq"
set billingtrans = server.createobject("adodb.recordset")
billingtrans.open sqlstr, dbconn
%>

<table id="billing">
        <tr>
            <td>Customer</td>
            <td>City</td>
            <td>Update</td>
            <td>Inquiry</td>
            <td>Total</td>
        </tr>
<% while not billingtrans.eof %>
        <tr>
            <td><% =billingtrans("customerName") %></td>
            <td><% =billingtrans("city") %></td>
            <td><% =billingtrans("Update") %></td>
            <td><% =billingtrans("Inquiry") %></td>
            <td><% =billingtrans("Total") %></td>
        </tr>
<% billingtrans.movenext
    wend
    billingtrans.close
    dbconn.close
%>
</table>
</body>
</html>

对此的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

移动你的所有&#34;循环之外的选项。

              <select Name="customersel" id="customersel">
                <% if Request.Form("customersel") = "0" then %>
                <option value="0" selected>All
                <% else %>
                <option value="0">All
                <% end if 
                do until customersel.EOF%>
                <option value="<%= customersel("customername") %>"><%= customersel("customername") %></option>

                <%
                customersel.MoveNext
                Loop %>
              </select>

NB do until...loopwhile not...wend的替代方案。两者都是有效的,这只是一个偏好问题。

您实际上并不需要条件语句来添加selected属性。作为你的所有&#34;选项是列表中的第一个选项,只要没有选择其他选项,它就会显示。