日期:2014-05-16  浏览次数:20360 次

JS 将数据库取出的数据封装成对象存入数组,取不出
JScript code

var dataSource = "d:/share/booksystem.accdb";
var conn;
var rs;

//返回ResultSet对象
function getResultSet(){
    conn=new ActiveXObject("Adodb.Connection");
    rs=new ActiveXObject("Adodb.Recordset");
    conn.ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+dataSource
                          +";Persist Security Info=False;"+"";
                          
    conn.open();
    rs.LockType=rs.CursorType=1;
    rs.ActiveConnection=conn;
    return rs;
}

//定义Book类
function Book(name,time,memo,num){
    this.name = name;
    this.time = time;
    this.num = num;
    this.memo = memo;
    
    this.getBK_Name = function(){
        return this.name;    
    }
    
    this.getBK_Time = function(){
        return this.time;    
    }
    
    this.getBK_Num = function(){
        return this.num;    
    }
    
    this.getBK_Memo = function(){
        return this.memo;    
    }
}

function queryBook(){
    var bookArray = new Array();
    rs = getResultSet();
    rs.Source="select * from book";
    rs.open();
    while(!rs.eof){
      var b = new Book(rs('bk_name'),rs('bk_dlv_time'),rs('bk_dlv_num'),rs('bk_dlv_comment'));
      bookArray.push(b);
           rs.moveNext();
    }
    
    alert(bookArray.length);
    var elm = bookArray[0];
    alert(elm instanceof Book);
    alert(elm.getBK_Name());   //本行报错:BOF 或 EOF 中有一个是“真”,
                       //或者当前的记录已被删除,所需的操 作要求一个当前的记录。



------解决方案--------------------
rs.LockType=rs.CursorType=1;

这一行没写错?

还有, var bookArray = new Array(); 
这个应该定义到函数体外面吧

------解决方案--------------------
JScript code
var b = new Book(
   String(rs('bk_name')),
   String(rs('bk_dlv_time')),
   String(rs('bk_dlv_num')),
   String(rs('bk_dlv_comment'))
);