application.cfc的问题显示数据源的消息“变量PRIMARYDATASOURCE未定义”

时间:2015-12-16 18:39:45

标签: coldfusion cfc application.cfc

当我尝试在index.cfm中触发简单的select查询时,使用Application.cfc中定义的数据源会显示错误消息。任何人都可以帮助我正确的代码序列吗?

错误:

  

变量PRIMARYDATASOURCE未定义

的Application.cfc

<cfcomponent>

    <!--- Set up the application. --->
    <cfscript>
        this.name = ""; // app name from old cfapplication tag
        this.sessionManagement = "Yes";
        this.loginstorage="session";
        this.setClientCookies = "Yes";
        this.primarydatasource = "diet";
        application.PRIMARYDATASOURCE = "diet";
    </cfscript>

    <cfsetting requesttimeout="100000" />

    <cffunction name="onApplicationStart">
            ....

        <cfquery datasource="#application.PRIMARYDATASOURCE#" name="getCostDetails">
            select * from maimun.adCostDetails ORDER BY ID DESC
        </cfquery>
            ....
    </cffunction>

</cfcomponent>

1 个答案:

答案 0 :(得分:6)

将行application.PRIMARYDATASOURCE = "diet";移至onApplicationStart功能并删除行this.primarydatasource = "diet";。完成。

放置在Application.cfc正文中的代码根本不会按照您的想法执行。一旦应用程序初始化,您只能访问application范围 - onApplicationStart就是此事件。将其视为Web应用程序的构造函数。