How can i take connection string?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi friends,
I'm new in C# and MVC. I'm using nop V2.3 and try to write a new plugin. I must connect the database and make some changes.

When i write connectionstring by manually in my plugin, it is work. But this method is not usefull for all person's usage.

I try these things:

1
Nop.Core.Data.DataSettings dbsttngs = default(Nop.Core.Data.DataSettings);
SqlConnection sqlConnection1 = new SqlConnection(dbsttngs.DataConnectionString.ToString());
.
.
.
//did not work. Or i made some mistakes.


2
StreamReader re = File.OpenText("~/App_Data/Settings.txt");
string input="";
string constring = "";
string a = "";
while ((input = re.ReadLine()) != null)
{
  if (input.Contains("DataConnectionString")) {
    a = input;
    a = a.Replace("DataConnectionString: ", "");
    a = a.Replace("DataConnectionString:", "");
    constring = a;
    }
  }
re.Close();
SqlConnection sqlConnection1 = new SqlConnection(constring);
.
.
.
//did not work because program couldn't find settings.txt file


Please help me how can i get connectionstring in V2.3? Sorry for my bad English.
12 years ago
var connectionString = new DataSettingsManager().LoadSettings().DataConnectionString;
12 years ago
Thank you Andrei. Your code is very usefull.
Before your writing, i discuss the issue like this: HostingEnvironment.MapPath("~/App_Data/Settings.txt")  read txt file, find dataconnection, read connection string, bla bla bla
12 years ago
Using nopCommerce 2.5

I found in the Nop.Web.Framwork.DependencyRegistrar the following line:

builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();


So, in my custom ImportManager (which itself is registered in a custom DependencyRegistrar) I have something like this:


private readonly DataSettings _dataSettings;

public CustomImportManager(DataSettings dataSettings)
{
this._dataSettings = dataSettings;
}

public void SomeMethod()
{
string connectionString = _dataSettings.DataConnectionString;
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.