用js操作mdb数据库的一些操作,包括数据的读取和存储;
html页面信息
?
?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}