title: Pass Connection Settings file to DatabaseProviderFactory, Ent Library
body: <p>I'm using .NET 3.5 / C# and the problem I face is that I need to be able to encrypt and decrypt a .config file which holds all my database connections to be used with the EnterpriseLibrary.Data.DatabaseProviderFactory, shown at end of question.
  This encryption and decryption is custom and is done with a known key and has to be the same for every machine hence the reason why i'm not using ProtectSection.</p>
  
  <p>I'm trying to read an encrypted configurationSetings.config file, decrypt the file in memory and pass the decrypted
  content of that file to the DatabaseProviderFactory so that it is able to access the database connection settings.</p>
  
  <p>I'm currently (non encrypted config file) using the MS Enterprise Libraries (v2) DatabaseProviderFactory in conjunstion with the FileConfigurationSource class to look at the App.config file for the name of the ConnectionSettings.config file, in which I store database connection settings.</p>
  
  <p>I then use pass the path of the ConnectionSettings file to the FileConfigurationSource and then I pass the result to the DatabaseProviderFactory
  which allows me to create a database instance.</p>
  
  <p>I don't want to have to write the unencrypted file to the filesystem and reading it back.
  I was looking that the option of using isolated storage for this but as I understand it you are unable to get a direct path to a file created using Isolated Storage.</p>
  
  <p>Perhaps there's a way to create a custom ConfigurationSource or something that will allow me to pass the decrypted into directly into the DatabaseProviderFactory in order to create a database instance.</p>
  
  <p>So in essence I need to read an encrypted configurationSetings.config file, decrypt the file in memory and pass the decrypted
  content of that file to the DatabaseProviderFactory so that it is able to access the database connection settings, or something similar so that I can map to Microsoft.Practices.EnterpriseLibrary.Data.Database.</p>
  
  <p>Any code and/or advice would be much appreciated.</p>
  
  <p>Thanks in Advance.</p>
  
  <p>Dave</p>
  
  <h2>APP.CONFIG</h2>
  
  <pre><code>  &lt;enterpriseLibrary.ConfigurationSource selectedSource="fileSource"&gt;
      &lt;sources&gt;
        &lt;add
          name="fileSource"
          type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5b2b86dc1561d89b"
          filePath ="ConnectionSettings.config" /&gt;
        &lt;add
          name="systemSource"
          type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5b8b89dc1061d43b" /&gt;
      &lt;/sources&gt;
    &lt;/enterpriseLibrary.ConfigurationSource&gt;
  </code></pre>
  
  <hr>
  
  <h2>METHOD CODE:</h2>
  
  <pre><code>Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource fileConfig =
     new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource(connectionSettingsConfigFile);
  
  Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory dbProvider =
     new Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory(fileConfig);
  
  if (string.IsNullOrEmpty(databaseConnectionName))
  {
     database = dbProvider.CreateDefault();
  }
  else
  {
     database = dbProvider.Create(databaseConnectionName);
  }
  </code></pre>
  
  <hr>
  
  <h2>CONNECTIONSETTINGS.CONFIG</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
  
    &lt;configSections&gt;
      &lt;section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5b2b86dc1561d89b" /&gt;
    &lt;/configSections&gt;
  
    &lt;dataConfiguration defaultDatabase="defaultConnection" /&gt;
    &lt;connectionStrings&gt;
      &lt;add name="defaultConnection" connectionString="Data Source=192.11.111.11;Initial Catalog=TestDB;User Id:Test;Password:test"
          providerName="System.Data.SqlClient" /&gt;
  
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <hr>
  
  <p>It seems that instead of using the DatabaseProviderFactory, if it's changed to use a specific implementation such as SqlDatabase it allows you to pass in the connectionString as part of its constructor, which allows a bit more flexible. So since i'm now using SqlDatabase what i need to figure out now is how to pass the contents of the decrypted config file, as a string, into a Configuration object. Any Ideas?</p>