问题
I have a Class Library, which inside just has a DataSet (MySQL connector) and a Connector class.
I use this class in multiple projects to connect to the database, and I always had the password embedded in the connection string but now I need to be able to modify this string(for security purposes) so I can have the user connect using their own account.
How can I modify this connection string.
I have tried the following
Properties.Settings.Default.DataBaseConnectionString = "String";
But it seems that the connection string is readonly becase it doesn't appear to have a setter value.
I also tried the following with no luck
Properties.Settings.Default.DatabaseConnectionString.Insert(
Properties.Settings.Default.DatabaseConnectionConnectionString.Length - 1,
"Password=dbpassword;");
回答1:
You can modify them like this:
Properties.Settings.Default["MyConnectionString"] = newCnnStr;
For a full solution that also saves the new value to the 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 another assembly, you can still do this providing you make the settings for that assembly public. To do this:
- Right-click the project in the solution explorer and click Properties
- Click the Settings tab.
- Change the Access Modifier dropdown to "Public", save, and close.
Then you can do this (assuming the other 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;
}
回答2:
Don't you have the source code of that class?
Also, but a little more complicated method, would be to patch the assembly using Reflector with the Reflexil AddIn.
回答3:
I think you're asking how you can change connectionstring properties at runtime depending on who is using the application. Hope this helps.
In the past I have done this by making my connection string contain parameters 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);
回答4:
It looks like you are loading the connection string from the config file, you should be able to change it from there. Once built it will be a file named the same as your compiled form plus .config. (For example application.exe.config)
来源:https://stackoverflow.com/questions/2592905/c-sharp-modify-connection-string-that-exists-inside-a-library