入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

Javascript读取ACCESS数据库

创建时间:2007-08-15 投稿人: 浏览次数:7105

 这些操作也许用不上,但也帖上来,网上也有很多相关例子,不多说帖出我自己改写的一段,欢迎指正

说明:
在存html文件的目录下有一个存放数据库的子目录:webData,其中的数据库名为scData.mdb
数据库中有一个userlist的表,有userid和username两个字段,并有一条userid="999",username="smallcol"的测试数据

js代码:
文件scEngine.js
//仿数据库连接池类
function scDBPool(){
   
try{
        
this.con=new ActiveXObject("ADODB.Connection");
         this.con.Provider="Microsoft.Jet.OLEDB.4.0";
         this.rs=new ActiveXObject("ADODB.Recordset");
    }catch(e){
         this.con=null;
         this.rs=null;
    }
    this.filePath=null;
    this.dbPath=null;
};


//设置数据库文件相对(定位文件)路径和数据库名
scDBPool.prototype.setDB=function(dbPath){
    this.dbPath=dbPath;
};

//设置数据库定位文件,这一步可以进连接类中,这里写是方便使用任何名字的数据库
scDBPool.prototype.setDBPathPosition=function(urlFile){
    var filePath=location.href.substring(0, location.href.indexOf(urlFile));
    this.dbPath=(this.dbPath==null||this.dbPath=="") ? "/webData/scData.mdb" : this.dbPath;
    var path=filePath+this.dbPath;
   //去除path前面的"files://"字符串
    this.filePath=path.substring(8);
};

//同数据库建立连接
scDBPool.prototype.connect=function(){
    this.con.ConnectionString="Data Source="+this.filePath;
    this.con.open;
};

//执行数据库语句返回结果集
scDBPool.prototype.executeQuery=function(sql){
    this.rs.open(sql,this.con);
};

//执行数据库语句不返回结果集
scDBPool.prototype.execute=function(sql){
    this.con.execute(sql);
};

//关闭结果集
scDBPool.prototype.rsClose=function(){
    this.rs.close();
    this.rs=null;
};

//关闭数据连接
scDBPool.prototype.conClose=function(){
    this.con.close();
    this.con=null;
};

html文件中调试应用,页面aa.html为数据库相对定位文件
<script language="javascript">
var db=new scDBPool();
db.setDBPathPosition("aa.html");
db.connect();
var sql="select * from [userlist] where userid = "999"";
db.executeQuery(sql);
while(!db.rs.eof){
    var cnt = db.rs.Fields("username");
    document.write(cnt);
    db.rs.moveNext;
}
db.rsClose();
db.conClose();
</script>

 

以上为个人参考网上改写,有错误的地方请指正,谢谢,本人正在不断学习中

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像