Skip to content Skip to sidebar Skip to footer

Pass Date To Adodb.command Parameter From Javascript

Using old-timey classic ADO, not ADO.NET, how do you pass a date value to a stored procedure from JavaScript? The JS is running in a classic ASP page on IIS7. SQL Server is 2012 (1

Solution 1:

Figured it out; I was so wrong, I didn't even mention the part that was actually causing the problem.

rs.Fields.Item("d").Value returns a variant of type adDBTimeStamp.

This is in an ASP that's acting as a web service and returning JSON, and the JSON stringifier I'm using just ignores properties with adDBTimeStamp values. Everything was coming back fine from the DB, then getting dropped later.

It actually turns out that ADODB.Command's CreateParameter method is very obliging about handling dates.

So:

var rs = RecordSetToObjArray(cmd.Execute();

//  ...//  Convert variant dates into something the JSON stringifier groks. functionGetADOFieldValue(field) {
    switch (field.Type) {
        case adDBTimeStamp:
        case adDate:
        case adDBDate:
        case adDBTime:
        case adFileTime:
            if ('undefined' === '' + field.Value)
                returnnull;
            returnnew Date('' + field.Value);

        default:
            return field.Value;
    }
}

//  Given recordset from ADODBCommand.Execute(), return as array of JSON //  objects. //  Also convert variant dates into something the JSON stringifier groks. //  If an SP returns multiple recordsets, that's on you. functionRecordSetToObjArray(rs) {
    var rtn = [];
    var fieldNames = [];

    for (var i = 0; i < rs.Fields.Count; ++i) {
        fieldNames.push(rs.Fields.Item(i).Name);
    }

    rtn.FieldNames = fieldNames;

    while (!rs.EOF) {
        var rec = {};

        for (var i = 0; i < fieldNames.length; ++i) {
            rec[fieldNames[i]] = GetADOFieldValue(rs.Fields.Item(fieldNames[i]));
        }
        rtn.push(rec);
        rs.MoveNext();
    }

    return rtn;
}

functionRecordSetToScalar(rs) {
    if (rs.RecordCount == 0 || rs.Fields.Count == 0)
        returnnull;
    return GetADOFieldValue(rs.Fields.Item(0));
}

Post a Comment for "Pass Date To Adodb.command Parameter From Javascript"