使用sql数据使用vbscript填充下拉列表

时间:2015-04-06 15:20:18

标签: sql vbscript

所以我有这个代码使用vbscript填充选择下拉列表,这是无效的任何帮助都会很棒。我也认为不应该使用这种反应。

 Set myConn = CreateObject("ADODB.Connection")
Set rec = CreateObject("ADODB.RecordSet")
With myConn
   .Provider = "SQLOLEDB"
   .Properties("Data Source") = "**"
   .Properties("Initial Catalog") = "***"
   .Properties("User ID") = "**"
   .Properties("Password") = "**"
   .Open
End With
   queryEmp = "SELECT Name as FN FROM dbo.Employee where Active='True'"
    rec.open(queryEmp,myConn)
    Do Until rec.EOF
    msgBox = rec.Fields("FN")
    strExample = rec.Fields("FN")
         response.write "<OPTION>" & strExample & "</OPTION>"
    End If
    rec.MoveNext
    Loop

1 个答案:

答案 0 :(得分:1)

为了避免安全问题,我在本演示中使用了.HTA:

<html>
 <head>
  <title>SQL DropDown Demo</title>
  <hta:application
     id="demo"
  ></hta>
  <script type="text/vbscript">

Option Explicit

Sub Window_OnLoad
  Dim oDD : Set oDD = document.GetElementById("ddSQL")
  Dim oDb : Set oDb = CreateObject("ADODB.Connection")
  oDb.Open "valid connection string"
  Dim sQL : sQL     = "valid SQL query"
  Dim oRS : Set oRS = oDb.Execute(sQL)
  Dim oOpt
  Do Until oRS.EOF
      Set oOpt = oDD.document.createElement("option")
      oOpt.Text  = oRS.Fields("valid column name").Value
      oOpt.Value = oOpt.Text
      oDD.Options.Add oOpt
      oRS.MoveNext
  Loop
End Sub

  </script>
 </head>
 <body>
  <form>
   <select id="ddSQL">
   </select>
  </form>
 </body>
</html>

证据:

Evidence

关键概念:清空选择元素,使用结果集循环创建选项,设置其属性,将它们添加到选择中。