HTML应用程序和数据库

时间:2011-09-16 07:53:43

标签: hta

如何访问HTA文件中的数据库?或者更好的是,访问任何COM? 我熟悉AutoIt,AutoHotKey和Windows Script Host。有没有办法在HTA应用程序中包含任何这些?

3 个答案:

答案 0 :(得分:1)

您可以使用与VBScript中完全相同的方式执行此操作。以下是使用Excel创建电子表格的示例。

要访问数据库,可以使用ADODB对象,要创建数据库,可以使用ADOX对象。您需要知道所需数据库类型的正确连接字符串。

<html>

<!-- COMTest.hta -->

<head>
<hta:application 
  id="oHTA"     
  border="thick"     
  borderstyle="raised"
  caption="yes"   
  maximizebutton="no"   
  minimizebutton="yes"
  showintaskbar="yes"   
  singleinstance="yes"   
  sysmenu="yes"   
  version="0.1"
  windowstate="normal" 
/>

<title>COM Test</title>

<script language="VBScript">

sub say(s)
  output.innerHTML = output.innerHTML & s & "<br>"
end sub

sub ComTest()
  say "testing COM"

  xlFile = "c:\test\ExcelTest.xls"
  ' use .xslx if you have Office 2007 or greater

  set fso = CreateObject("Scripting.FileSystemObject")
  if fso.FileExists(xlFile) then
    say "deleting test file: " & xlFile
  end if

  say "creating Excel Application object and workbook"
  set oEx = CreateObject("Excel.Application")
  set oWb = oEx.Workbooks.Add() ' create a new workbook
  set oWs = oWb.Worksheets(1) ' point to first worksheet

  oWs.cells(1,1) = "Test Worksheet"
  oWs.cells(2,1) = "=now()"
  oWs.UsedRange.Columns.AutoFit

  say "saving test file: " & xlFile
  oEx.DisplayAlerts = false ' if file exists, overwrite it without prompting
  oWb.SaveAs xlFile

  oEx.Quit
  set oEx = nothing

  say "done"
end sub

</script>

<style type="text/css">
body {
  overflow: auto;
  background-color: "blanchedalmond";
}

#output {
  color: lightgreen;
  background-color: black;
  font-family: "Lucida Console";
  font-size: 9pt;
  padding: 3px;
}
</style>
</head>

<body>
<input type="button" value="test" onclick="ComTest">
<br>
<pre id="output"></pre>
</body>

<script language="vbscript">
sub ShowTitle()
  say document.Title
  say "command line=" & oHTA.commandLine
end sub

ShowTitle
</script>

</html>

答案 1 :(得分:0)

要访问数据库,您需要ActiveXObject。

var conn    = new ActiveXObject("ADODB.Connection");
var rs      = new ActiveXObject("ADODB.Recordset");
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=handbook.mdb");
rs.Open("select * from faq", conn, 3, 2);
if (!rs.BOF && !rs.EOF) {
    questionField.value = rs.fields('question').value;;
    answerField.value = rs.fields('answer').value; 
}

答案 2 :(得分:0)

我拿了两个答案并合并如下:

a)我从excel测试中更新了ComTest()以访问oracle db
b)ActiveXObject仅适用于JScript,因此我按照以下方式将其转换为VBscript:http://msdn.microsoft.com/en-us/library/ms756007(v=vs.85).aspx

sub ComTest()
  say "testing COM"

  dim conn, rs 
  set conn = CreateObject("ADODB.Connection")
  Set   rs = CreateObject("ADODB.Recordset")
  conn.Open("Provider=OraOLEDB.Oracle;Data Source=XXXX;User ID=XXXX;Password=XXXX")
  say "open conn"
  rs.Open "select sysdate from dual", conn
  say  "sqlResult =" & rs.Fields("sysdate").Value

  'Close connection and clean up objects
  conn.Close
  say "close conn"
  Set rs = Nothing
  Set conn = Nothing

  say "done"
end sub