C # Change the connection string that exists inside the library

I have a class library inside which there is only a DataSet (MySQL Connector) and a Connector class.

I use this class in several projects to connect to the database, and I always had a password built into the connection string, but now I need to be able to change this string (for security purposes) so that I can connect to the user using my own account .

How to change this connection string.

I tried the following

Properties.Settings.Default.DataBaseConnectionString = "String"; 

But it seems that the connection string is read-only, because it does not matter the setting.

I also tried the following with no luck

 Properties.Settings.Default.DatabaseConnectionString.Insert( Properties.Settings.Default.DatabaseConnectionConnectionString.Length - 1, "Password=dbpassword;"); 
+1
source share
4 answers

You can change them as follows:

 Properties.Settings.Default["MyConnectionString"] = newCnnStr; 

For a complete solution that also saves the new value in a file, you need to do something like this:

  private static void ModifyConnectionStrings() { // Change the value in the config file first var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); const string newCnnStr = "server=(local);database=MyDb;user id=user;password=secret"; config.ConnectionStrings.ConnectionStrings["MyProject.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr; config.Save(ConfigurationSaveMode.Modified, true); // Now edit the in-memory values to match Properties.Settings.Default["MyConnectionString"] = newCnnStr; } 

If your dataset is in a different assembly, you can still do this by providing public settings for this assembly. For this:

  • Right-click the project in Solution Explorer and select Properties
  • Go to the Settings tab.
  • Change the Access Modifier drop-down menu to "Public", save and close.

Then you can do this (assuming another project is called "MyProject.DataLayer"):

  private static void ModifyConnectionStrings() { // Change the value in the config file first var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); const string newCnnStr = "server=(local);database=MyDb;user id=user;password=secret"; config.ConnectionStrings.ConnectionStrings["MyProject.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr; config.ConnectionStrings.ConnectionStrings["MyProject.DataLayer.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr; config.Save(ConfigurationSaveMode.Modified, true); // Now edit the in-memory values to match Properties.Settings.Default["MyConnectionString"] = newCnnStr; MyProject.DataLayer.Properties.Settings.Default["MyConnectionString"] = newCnnStr; } 
+5
source

Don't have the source code for this class?

Also, but a bit more complicated method: fix the build using Reflector with Reflexil AddIn .

0
source

I think you are asking how you can change the properties of the connection string at runtime depending on who is using the application. Hope this helps.

In the past, I did this by setting parameters in the connection string that I can provide using string.Format.

  <connectionStrings> <add name="SomeDB" connectionString="("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password={1}"" /> </connectionStrings> string connectionString = string.Format(ConfigurationManager.ConnectionStrings["SomeDB"].ConnectionString, location, password); 
0
source

It looks like you are loading the connection string from the configuration file, you can change it. After creating it, there will be a file with a name similar to your compiled form plus .config. (For example, application.exe.config)

-1
source

Source: https://habr.com/ru/post/1336997/


All Articles