通过Javascript访问数据库

时间:2017-03-17 16:22:48

标签: javascript ms-access prototype hta

我正在创建一个HTA应用程序,因为我的环境有限制。在任何情况下,我都试图通过原型连接到Access数据库。这个原型假设从数据库中获取信息,但它总是返回" undefined"。我无法确定为什么会发生这种情况,因为我正在使用打开该提供程序的ActiveX控件。

通话功能 请注意,安装了访问驱动程序,位置正确,表格中存在内容。

function GetX()
{
    var db = new AccessDatabase("I:\\Office\\Access\\Sample.accdb");
    db.Connect();

    if (db.Status != 0) { return; }

    // Remove all
    $("#x").empty();

    // The Query
    var results = db.Select("SELECT x FROM Tracker");

    // Get the results length
    var arrayLength = results.length;

    // Iterate through the results
    for (var row = 0; row < arrayLength; row++)
    {
        $("#x").append("<option id='" + results[row][0] + ">" + results[row][0] + "</option>");
    }

    db.Disconnect();
}

原型对象:

function AccessDatabase(databaseSource)
{
    try
    {
        this.Connection = new ActiveXObject("ADODB.Connection");
        this.Source = databaseSource;
        this.Status = 0;
    }
    catch (err)
    {
        createNotification("Danger", "Unable to create ActiveX Control For Microsoft ");
        this.Status = err.Number;
    }
}

AccessDatabase.prototype.Connect = function()
{
    try
    {
        var provider = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + this.Source;
        this.Connection.Open(provider);
    }
    catch (err)
    {
        createNotification("Danger", "Database Failed To Connect. Was the Source correct?<br>Details: " + err.message);
        this.Status = err.Number;
    }
}

AccessDatabase.prototype.Disconnect = function()
{
    this.Connection.Close();
}

AccessDatabase.prototype.Select = function (selectQuery)
{
    try
    {
        // Open the recordset
        var recordSet = new ActiveXObject("ADODB.Recordset");
        recordSet.Open(selectQuery, this.Connection);

        // Check for null results
       if (recordSet.RecordCount == null || recordSet.RecordCount == -1)
       {
            createNotification("Error", "No record was found for the query provided <br> Query: <small>"
            + selectQuery + "</small>");

            recordSet.Close();

            this.Status = -1;
            return null;
        }

        alert("Record Count:" + recordSet.RecordCount);

        // Convert To Array
        var returnValue = recordSet.GetRows();
        recordSet.Close();

        // Return the array
        return returnValue;
    }
    catch (err)
    {
        createNotification("Warning", "Query Could Not Execute Due to an error. <br>Details: " + err.message);
        this.Status = err.Number;
    }
}

0 个答案:

没有答案