javascript mysql读写_Javascript读取ACCESS数据库 | 学步园

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

说明:

在存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为数据库相对定位文件

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();

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值