开发者

HTML Application and databases

开发者 https://www.devze.com 2023-04-05 11:58 出处:网络
How to access database in HTA files? Or better yet, access any COM? I\'m familiar with AutoIt, AutoHotKey and Windows Script Host. Is there a way开发者_开发知识库 to include any of these in an HTA app

How to access database in HTA files? Or better yet, access any COM? I'm familiar with AutoIt, AutoHotKey and Windows Script Host. Is there a way开发者_开发知识库 to include any of these in an HTA app?


You do it in exactly the same way as you would in VBScript. Below is an example of creating a spreadsheet using Excel.

To access databases, you can use the ADODB object, and to create a database, you would use the ADOX object. You need to know the right connection string for the type of database you need.

<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>


To access the database, you will need the 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; 
}


I took the two answers and merged them as follows:

a) I updated ComTest() from an excel test to access an oracle db
b) ActiveXObject only works for JScript, so I converted it to VBscript as per: 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
0

精彩评论

暂无评论...
验证码 换一张
取 消