16

Searched google and using Enterprise library data access to connect database.

Installed only data access pack using https://www.nuget.org/packages/EnterpriseLibrary.Data/.

After added to the project, I've set the configuration as follows,

     <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    </configSections>
  <dataConfiguration defaultDatabase="dProvider" />
    <connectionStrings>
        <add name="dProvider" connectionString="server=local;Initial Catalog=n;uid=sa;pwd=pwd"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

Called through the application like the following,

Database db;
            string sqlCommand;
            DbCommand dbCommand;

            db = DatabaseFactory.CreateDatabase("dProvider"); or DatabaseFactory.CreateDatabase();

After run the application, I got the following exception,

{"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."}

What mistake I made ? How to solve this issue ?

1 Answer 1

23

Finally found the answer. It has been occurred because of the configuration section.

I've used version 6, but here I've mentioned like version 5 in the configuration section. So the error has occurred.

I've replaced the configuration section like following, It worked perfectly in good way. :-). Thanks a lot for the helpers.

<configSections>
         <section name="dataConfiguration" 
      type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, 
            Microsoft.Practices.EnterpriseLibrary.Data"/>
    </configSections>

and used DataBaseProviderFactory class to create instance.

DatabaseProviderFactory factory = new DatabaseProviderFactory();

            db = factory.Create("dProvider");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for figuring this out! It was just what I needed. I found out the following also worked: DatabaseProviderFactory factory = new DatabaseProviderFactory(); Database db = factory.CreateDefault(); DbCommand command = db.GetStoredProcCommand("stored proc name"); <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data"/> </configSections> <dataConfiguration defaultDatabase="ConnString"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.