C# Modify Connection String that exists inside a l

2019-06-04 19:08发布

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;");

4条回答
神经病院院长
2楼-- · 2019-06-04 19:24

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);
查看更多
▲ chillily
3楼-- · 2019-06-04 19:44

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.

查看更多
成全新的幸福
4楼-- · 2019-06-04 19:46

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)

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-06-04 19:49

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:

  1. Right-click the project in the solution explorer and click Properties
  2. Click the Settings tab.
  3. 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;
    }
查看更多
登录 后发表回答