Skip to content

Instantly share code, notes, and snippets.

@skiningham
Last active September 26, 2016 21:24
Show Gist options
  • Save skiningham/c12e5f0f45d88df20367 to your computer and use it in GitHub Desktop.
Save skiningham/c12e5f0f45d88df20367 to your computer and use it in GitHub Desktop.
Rough example of nopCommerce DataSettingsManager using web.config
using System;
using System.Web;
using System.Web.Configuration;
namespace Nop.Core.Data
{
/// <summary>
/// Manager of data settings (connection string)
/// </summary>
public partial class DataSettingsManager
{
/// <summary>
/// Load settings
/// </summary>
/// <returns></returns>
public virtual DataSettings LoadSettings()
{
try {
System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
return new DataSettings
{
DataConnectionString = webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ConnectionString,
DataProvider = webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ProviderName
};
}
catch (NullReferenceException) {
return new DataSettings();
}
}
/// <summary>
/// Save settings to a file
/// </summary>
/// <param name="settings"></param>
public virtual void SaveSettings(DataSettings settings)
{
if (null == settings) throw new ArgumentNullException("settings");
System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ConnectionString = settings.DataConnectionString;
webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ProviderName = settings.DataProvider;
webConfig.Save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment