问题
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?
回答1:
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.
回答2:
Just do two steps
Replace two method
LoadSettings
andSaveSettings
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(); }
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>
回答3:
In addition to adding the connection to the web.config, you have to specify the providerName="sqlserver".
Ex) ;Initial Catalog=;Integrated Security=False;User ID=;Password=;Connect Timeout=30;Encrypt=True" providerName="sqlserver" />
This is because the EfDataProviderManager in Nop.Data has a check for the provider name, and will throw an exception if you put the normal
providerName="System.Data.SqlClient"
回答4:
Please add this to your web.config under Nop.Web project :
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=serverName;Initial Catalog=DBName;Persist Security Info=False;UserID=userName;Password=password"
</connectionStrings>
Best Regards.
来源:https://stackoverflow.com/questions/29473901/change-the-connection-string-of-nopcommerce