企业库数据块抛出异常

时间:2017-03-29 17:14:29

标签: asp.net

我正在开发一个新的应用程序,只需使用以下命令通过nuget下载企业库:

Install-Package EnterpriseLibrary.Data

每当我尝试调用以下语句时,我都会收到以下错误:

 Database objDB = DatabaseFactory.CreateDatabase();

例外是

应用程序中的服务器错误。

Database provider factory not set for the static DatabaseFactory. Set a provider factory invoking the DatabaseFactory.SetProviderFactory method or by specifying custom mappings by calling the DatabaseFactory.SetDatabases method. 

我的配置文件如下所示:

 <configSections>

<!--<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.1304.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" ></section>-->
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data"/>

我的连接字符串如下:

  <connectionStrings>
<add name="Dbconnection"
   connectionString="Server=SQL100; Database=Test_db ; Trusted_Connection=Yes; "
   providerName="System.Data.SqlClient"   />

我为调用存储过程而创建的方法如下:

 string connStr = ConfigurationManager.AppSettings["ConnString"].ToString();
 DataSet ds = new DataSet();

        Database objDB = DatabaseFactory.CreateDatabase();

        DbCommand cmd;

        try

        {

            cmd = objDB.GetStoredProcCommand("sp_PubTables");



            ds = objDB.ExecuteDataSet(cmd);

            return ds;


        }

        catch (Exception ex)

        {

            throw ex;

        }

任何帮助将不胜感激。我正在使用Visual Studio 2015.我的整个配置文件位于

之下
     <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>

    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.1304.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" ></section>
    <!--<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data"/>-->
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>

  <pages>
      <controls>
        <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
      </controls>
    </pages></system.web>
  <connectionStrings>
    <add name="Dbconnection"
       connectionString="Server=SQL100; Database=test_db ; Trusted_Connection=Yes; "
       providerName="System.Data.SqlClient"   />

  </connectionStrings>

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>

  </system.webServer>
</configuration>

1 个答案:

答案 0 :(得分:0)

尝试将此添加到<configuration>节点中的配置中:

<dataConfiguration defaultDatabase="Dbconnection" />