18Nov

javascript链接access数据库的方法

  1. <meta name="Generator" content="EditPlus" />
  2. <meta name="Author" />
  3. <meta name="Keywords" />
  4. <meta name="Description" />
  5. <script language="JavaScript">          <!--  function getCountFromDB() {      //以当前页面文件为基础,找到文件所在的绝对路径。      var filePath = location.href.substring(0, location.href.indexOf("Jsconnmdb.htm"));      var path = filePath + "ip.mdb";      //去掉字符串中最前面的"files://"这8个字符。      path = path.substring(8);      var up&#100;ateCnt = 0;
  6.  
  7. //生成查询和更新用的sql语句。      var sqlSelCnt = "sel&#101;ct COUNT FROM [COUNT] wh&#101;re ID = \\\\'count\\\\'";      var sqlUpdCnt = "up&#100;ate [COUNT] SET [COUNT] = \\\\'";
  8.  
  9. //建立连接,并生成相关字符串 www.knowsky.com。      var con = new ActiveXObject("ADODB.Connection");      con.Provider = "Microsoft.Jet.OLEDB.4.0";      con.ConnectionString = "Data Source=" + path;
  10.  
  11. con.open;      var rs = new ActiveXObject("ADODB.Recordset");      rs.open(sqlSelCnt, con);      while (!rs.eof) {          var cnt = rs.Fields("COUNT");          document.write(cnt);          //将取得结果加1后更新数据库。          up&#100;ateCnt = cnt * 1 + 1;          rs.moveNext;      }      rs.close();      rs = null;
  12.  
  13. sqlUpdCnt = sqlUpdCnt + up&#100;ateCnt + "\\\\'";      con.execute(sqlUpdCnt);
  14.  
  15. con.close();      con = null}
  16.  
  17. function getCountFromTxt() {      var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm"));      var path = filePath + "count.txt";      path = path.substring(8);      var nextCnt = 0;
  18.  
  19. var fso, f1, ts, s;      //以只读方式打开文本文件。      var ForReading = 1;      //以读写方式打开文本文件。      var ForWriting = 2;
  20.  
  21. fso = new ActiveXObject("Scripting.FileSystemObject");      f1 = fso.GetFile(path);      ts = f1.OpenAsTextStream(ForReading, true);      s = ts.ReadLine();      nextCnt = eval(s) + 1;      document.write("now count is :" + s);      ts.Close();
  22.  
  23. ts = f1.OpenAsTextStream(ForWriting, true);      ts.WriteLine(nextCnt);      ts.close()}  //--></script>
  24. <script language="JavaScript">          <!--  getCountFromTxt()//--></script>

Leave a Reply