Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the connection string of nopCommerce?

I am using nopCommerce and I need to remove the connection string in the settings.txt file and insert the web.config file. How can i do this?

like image 743
Everton Bezerra Avatar asked Dec 09 '22 04:12

Everton Bezerra


2 Answers

The most straightforward way to move the connection string out of settings.txt and into the web.config is to modify the Nop.Core.Data.DataSettingsManager. Specifically the LoadSettings() and SaveSettings() methods. You can store the connection string wherever you'd like (ideally in web.config), as long as those two methods read and write the configuration.

A rough example of the DataSettingsManager updated to support storing the connection string in web.config can be found in this Gist: http://git.io/vUPcI Just copy the connection string from settings.txt to web.config and name the connection "DefaultConnection" or adapt the code accordingly.

like image 125
Stephen Kiningham Avatar answered Feb 03 '23 17:02

Stephen Kiningham


Just do two steps

  1. Replace two method LoadSettings and SaveSettings in \nopCommerce\Libraries\Nop.Core\Data\DataSettingsManager.cs. Code from link of @Stephen Kiningham

    /// <summary>
    /// Load settings
    /// </summary>
    /// <param name="filePath">File path; pass null to use default settings file path</param>
    /// <returns></returns>
    public virtual DataSettings LoadSettings(string filePath = null)
    {            
        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();
    }
    
  2. Add connection string to your web config web.config

    <connectionStrings>
        <add name="DefaultConnection"
             connectionString=" Data Source=localhost;Initial Catalog=nopcommerce;Integrated Security=True;Persist Security Info=False"
             providerName="sqlserver">
        </add>
    </connectionStrings>
    
like image 24
user3774600 Avatar answered Feb 03 '23 17:02

user3774600