Javascript-处理mdb数据库

用js操作mdb数据库的一些操作,包括数据的读取和存储;


Javascript-处理mdb数据库html页面信息
 
Javascript-处理mdb数据库
 mdb数据库表一
Javascript-处理mdb数据库
mdb数据库表二 


具体代码如下:


//获得fileName的路径;
function serverMapPath(fileName) {
 var syspath = location.href;  //file:///c:/Documents%20and%20Settings/Administrator/桌面/新建文件夹/handle-mdb.html
 alert(syspath)
 syspath = syspath.toLowerCase();

 myPosition = syspath.lastIndexOf("/"); 

 syspath = syspath.substring(0, parseInt(myPosition) + 1); 

 syspath = syspath.replace("file:///", "");  

 syspath = syspath.replace(new RegExp("%20", "gm"), " ");  

 syspath = syspath + fileName;  //c:/documents and settings/administrator/桌面/新建文件夹/Database.mdb;
 alert(syspath)
 return syspath.toString();
}

 

//获取mdb数据库中的数据到html-方法1-多字段名称;
function loadData(LibleName) {
   var conn = new ActiveXObject("ADODB.Connection");
   var connStr = "DBQ=" + serverMapPath("Database.mdb") + ";DRIVER={Microsoft Access Driver (*.mdb)};"
   //打开数据库;
   conn.Open(connStr);
   var rs = new ActiveXObject("ADODB.Recordset");
   var sql = "select * from " + LibleName;
   //打开记录集;
   rs.open(sql, conn);
   while (!rs.EOF) {
      var feildName = "";
      var valueCell = "";
      var inputs  = document.getElementsByTagName("input");
      var textAreas = document.getElementsByTagName("textarea");
   //rs.fields.count为字段名称的总数;
      for(var z=0;rs.fields.count>z;z++){
   //filedName为字段名称;
         filedName = rs(z).Name;
   //mdb中各字段内存储的值;
         valueCell = (rs.Fields(filedName).Value);
         if (valueCell == null){
     valueCell = "";
   }
   for (var i = 0; i < inputs.length; i++) {
      var inputCell = inputs[i];   
      if(filedName==inputCell.id){
      inputCell.value=valueCell;
   }
   }
   for (var j = 0; j < textAreas.length; j++){
             var textAreaCell = textAreas[j];
             if (filedName == textAreas.id){
                  textAreaCell.value = valueCell;
             } 
         }
      }
      rs.moveNext();
   }
   rs.close;
   rs = null;
}

 

//获取mdb数据库中的数据到html-方法2-两个字段名称-一个ID,一个name;
function Show(tableName) {
 var str =tableName;
 var conn = new ActiveXObject("ADODB.Connection");
 var connStr = "DBQ=" + serverMapPath("Database.mdb") + ";DRIVER={Microsoft Access Driver (*.mdb)};"
 conn.Open(connStr);
 var rs = new ActiveXObject("ADODB.Recordset");
 var sql = "select * from "+str+" order by id";
 rs.open(sql, conn);
 theInput = null;
 while (!rs.EOF) {
  var htmlID = "";
  var value = "";
  //mdb中的ID是正整数,html中的ID由四位数字组成,位数不够的用0不足;
  //如mdb中的‘1’,html中是‘0001’;
  htmlID = rs.Fields("id").value;
  if(htmlID.length==1){
     htmlID = '000'+htmlID;
  }else if(htmlID.length==2){
     htmlID='00'+htmlID;
  }else if(s.length==3){
     htmlID='0'+htmlID;
  }
  try
  {
   theInput = document.getElementById(htmlID);
   if (theInput !== null){
    value = (rs.Fields("name").Value);
       if (theInput.type == "text"){
     theInput.value = value;
    }
    if (theInput.type == "textarea") {
     theInput.value = value;
    }
    if (theInput.type == "radio"){
     if (value == "√"){theInput.checked = true}
     if (value == "□"){theInput.checked = false}
    }
    if (theInput.type == "checkbox"){
     if (value == "√"){theInput.checked = true}
     if (value == "□"){theInput.checked = false}
    }

    if (theInput.name == "select") {
     for(j = 0; j< theInput.length ; j++){
      if (theInput.options[j].value == value) {
       theInput.options[j].selected = true;
      }
     }
    }
    //name是值为‘uu’的处理;
    if(theInput.className == "uu"){
       theInput.innerHTML = value;
    }
   }
  }
  catch(E)
  {
  }
  rs.moveNext();
 }
 conn.close();
 conn = null;
}

 

//把html中填写的内容更新到数据库;
function update(str) {
   var conn = new ActiveXObject("ADODB.Connection");
   var connStr = "DBQ=" + serverMapPath("Database.mdb") + ";DRIVER={Microsoft Access Driver (*.mdb)};"
   conn.Open(connStr);
  
   InputList = document.getElementsByTagName("input");
   TheInput = null;
   for(i = 0; i < InputList.length; i++) {
      TheInput = InputList[i];
   var TheInputId = TheInput.id;
   TheInputId = parseInt(TheInputId,10);
   if(!isNaN(TheInputId)){
         if (TheInput !== null){
      //radio单选框的处理
      if (TheInput.type == "radio"){
    var sql = "delete from "+ str +" where id = '"+ TheInputId+"'";
    conn.execute(sql);
       if (TheInput.checked == true){
       var sql = "insert into "+ str +" values("+ TheInputId +",'" + "√" + "')";
       conn.execute(sql);
       }
       if (TheInput.checked == false){
       var sql = "insert into "+ str +" values("+ TheInputId +",'" + "□" + "')";
       conn.execute(sql);
       }
      }
   //checkbox复选框的处理;
      if (TheInput.type == "checkbox"){
         var sql = "delete from "+ str +" where id = '"+ TheInputId+"'";
      conn.execute(sql);
      if (TheInput.checked == true){
         var sql = "insert into "+ str +" values("+ TheInputId +",'" + "√" + "')";
         conn.execute(sql);
      }
      if (TheInput.checked == false){
         var sql = "insert into "+ str +" values("+ TheInputId +",'" + "□" + "')";
         conn.execute(sql);
      }
      }
   //一般文本输入框处理
   if (TheInput.type == "text") {
    if (TheInput.value !== null){
     var sql = "delete from "+ str +" where id = '"+ TheInputId+"'";
     conn.execute(sql);
     var sql = "insert into " + str + " values(" + TheInputId + ",'" + TheInput.value + "')";
     conn.execute(sql);
    }
   }
   //隐藏域的处理
   if (TheInput.type == "hidden") {
    if (TheInput.value !== null){
     var sql = "delete from "+ str +" where id = '"+ TheInputId+"'";
     conn.execute(sql);
      sql = "insert into " + str + " values(" + TheInputId + ",'" + TheInput.value + "')";
     conn.execute(sql);
    }
   }
         }
      }
   }


   //textarea多行文本框;
   InputList = document.getElementsByTagName("textarea");
   TheInput = null;
   for(i = 0; i < InputList.length; i++) {
      TheInput = InputList[i];
      var TheInputId = TheInput.id;
      TheInputId = parseInt(TheInputId,10);
      if(!isNaN(TheInputId)){
         if (TheInput !== null){
            if (TheInput.text !== null){
               var sql = "delete from "+ str +" where id = '"+ TheInputId+"'";
               conn.execute(sql);
               var sql = "insert into "+ str +" values("+ TheInputId +",'" + TheInput.value + "')";
               conn.execute(sql);
            }
         }
      }
   }
  
   //select下拉框的处理
   InputList = document.getElementsByTagName("select");
   TheInput = null;
   for(i = 0; i < InputList.length; i++) {
      TheInput = InputList[i];
      var TheInputId = TheInput.id;
      TheInputId = parseInt(TheInputId,10);
      if(!isNaN(TheInputId)){
         if (TheInput !== null){
   if (TheInput.name == "select") {
    var sql = "delete from "+ str +" where id = '"+ TheInputId+"'";
    conn.execute(sql);
    for(j = 0; j< TheInput.length; j++){
     if (TheInput.options[j].selected) {
      var sql = "insert into "+ str +" values("+ TheInputId +",'" + TheInput.options[j].value + "')";
      conn.execute(sql);
     }
    }
   }
         }
      }
   }
   conn.close();
   conn = null;
}