質問

Maybe there is something better to achieve what I'm trying to do but I've been stuck trying to do this and nothing else comes to mind. I created an array of arrays, but I get an error if I print everything in the arrays and it only goes four times in the loop for some reason. It's getting the data from a record set in a database. Please help?

Here is my code:

var connection = new ActiveXObject("ADODB.Connection");
var connectionstring = "DSN=adsn;UID=root;PWD=1234";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");    
var arrayCol = new Array(17);
var arrayTable = [];
rs.MoveFirst(); //first value of the record set
//alert("Columns: "+columns); //should return 15
for(var ii=0;ii<3;ii++){
    for(var jj=0;jj<17;jj++){
        alert(jj);
        if(rs.Fields(jj).value == null && rs.Fields(jj).EOF != true){
            arrayCol[jj] = "";
        }
        else
            arrayCol[jj] = rs.Fields(ii).Value;
        rs.MoveNext();
    }
    alert(arrayCol[0]+" "+arrayCol[1]+" "+arrayCol[2]+" "+arrayCol[3]+" "+arrayCol[4]+" "+arrayCol[5]+" "+arrayCol[6]+" "+arrayCol[7]+" "+arrayCol[8]+" "+arrayCol[9]+" "+arrayCol[10]+" "+arrayCol[11]+" "+arrayCol[12]+" "+arrayCol[13]+" "+arrayCol[14]+" "+arrayCol[15]+" "+arrayCol[16]);
}

and as a result I get M M M undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded undefinded

M is the value of the first item in the array

It also gives me the error: Item cannot be found in the collection corresponding to the requested name or ordinal. after getting in the loop 4 times even if I change both inner and outter loop numbers to any number

役に立ちましたか?

解決

I have no idea of ActiveX and ADOB.Recordset, but the condition rs.Fields(ii).value == null && rs.Fields(ii).EOF != true looks odd. Shouldn't it rather be something like

if (rs.Fields(ii).EOF || rs.Fields(ii).value == null) {
    // then use empty string instead of .value
}

Btw, to get a table (nested arrays) you will need to add something like

var arrayCol = new Array(17);
arrTable.push(arrayCol);

to your outer loop.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top