`
huanlong78
  • 浏览: 33024 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

JavaScript ----- 操作 ACCESS 数据库疑问

阅读更多

  某天在网上闲逛的时候,发现有人贴出来的一段代码:

 

         con=new ActiveXObject("ADODB.Connection");
         con.Provider="Microsoft.Jet.OLEDB.4.0";
         rs=new ActiveXObject("ADODB.Recordset");

 

迷茫什么时候 JS 能操作数据库了 ???

 

然后开始搜索相关的信息,看到一人写的 Javascript For ACCESS 的数据库操作类:

 

 

function JDB(){
    try{
         this.con=new ActiveXObject("ADODB.Connection");
         this.con.Provider="Microsoft.Jet.OLEDB.4.0";
         this.rs=new ActiveXObject("ADODB.Recordset");
    }catch(e){
  // alert(e);
         this.con=null;
         this.rs=null;
    }
    this.filePath=null;
    this.dbPath=null;
};

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

//同数据库建立连接
JDB.prototype.connect=function(){
	//this.filePath="";    //access路徑
    this.con.ConnectionString="Data Source="+this.filePath + ";Jet OLEDB:Database Password='Cmanagers'";
    this.con.open;
};

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

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

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

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

 

 

自己测试了用下 , 可以用的 , 但是此类仅限于在本机上使用 , 因为路径 只能制定本地的数据库 , 不然报错 .

测试:

 

 

var DB = new JDB();
DB.setDB("D:/test/info.mdb");
DB.connect();
DB.Query("SELECT password FROM user_data");

alert( DB.rs(0) ) ;
alert( DB.rs('password') ) ;
alert( DB.rs.fields(0) ) ;
alert( DB.rs.fields('password') ) ;

 

分享到:
评论
1 楼 jforever 2009-12-10  
今天刚发现一个操作Access更方便的JavaScript Library。
http://www.open-open.com/ajax/ajax20091210161448.htm

相关推荐

Global site tag (gtag.js) - Google Analytics