1

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 Answers 4

5

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;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Just updated to account for connection strings for other assemblies (i.e. if you have a data layer project with the datasets in).
I didn't think using the [] indexer would make it work as I expected it using its value name. You don't understand how much that helped me :) I just finished migrating the database to MySQL for security reasons and couldn't figure out how to use different usernames/passwords in connection string at runtime.
0

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.

1 Comment

I do have the source code, its my own project. But I cannot figure out how to modify the connection string.
0

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 Comments

This seems perfect, but how can I make my dataset and all my adapters connect/reconnect using that string?
This won't work for var adapter = new MyTableAdapter() though, because that will load the values directly from the file. You'd have to explicitly create Connection objects and set the TableAdapter.Connection property each time.
If you look at Neil Barnwell's answer, you can use the Properties.Settings.Default["MyConnectionString"] and that automatically updates all tableadapters that use that connection string.
Cool, good to know. I think I missed that part of your question :/ Regardless I'm glad you have your answer.
-1

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)

3 Comments

I don't know why I got a down vote, it is possible to modify them at run time but typically for connection strings they are changed infrequently enough that a configuration change outside of the application works. Not saying it is the best answer but a downvote was harsh IMO
I didn't downvote you, but you need to read the question more carefully: "I need to be able to modify this string ... so I can have the user connect using their own account."
That exact string works perfectly for a business centric application with an IT staff to maintain it. Who would have no issue changing XML in a .config file. Or heck it could be set by an installation file, in either case modification outside of your application isn't a bad idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.