1

I am developing an winforms application in VS 2010 to retrive data from Sql Server or MySql to Sql Server or MySql.

My design is something like this.

Application Screenshot

So here I am storing all values of connection string in Sql Server database table in separate column.

I am able to get the required fields and validate them, test them and store it in database but I am stuck at how to get the stored connection string at runtime to work and how to use the last selected connection string?

guidance please.

9
  • Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. Commented Nov 28, 2013 at 6:58
  • 1
    Are you connecting to many different databases? Is there a central one? Connection strings are very easily managed by saving to a config file, then you dont need a connection string to find your connection strings. Commented Nov 28, 2013 at 7:01
  • @MikeW I have code to get the connection string, validate it and store it so no use of that code here I thought that's why I didn't provided here. I am not able to make design that how can I use last saved settings when application start. Thank you Commented Nov 28, 2013 at 7:05
  • 1
    Just because its saved in the config file, doesnt mean its hard coded. You can write to the config file as easily as you can read from it. Commented Nov 28, 2013 at 7:08
  • 1
    Perhaps the Settings class would appeal to you more. Commented Nov 28, 2013 at 7:14

1 Answer 1

2

Your config file might have a connectionStrings section like this:

<connectionStrings>
    <add name="ConnectionNumberOne"
        connectionString="Data Source=ds;Initial Catalog=DB;Integrated Security=True"
        providerName="System.Data.SqlClient" />
    <add name="ConnectionNumberTwo"
        connectionString="Data Source=ds2;Initial Catalog=DB2;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

You can read the connection string thusly:

var connectionOne = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionNumberOne"];
var connectionTwo = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionNumberTwo"];

And you can save the connection string as well:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["ConnectionNumberOne"].ConnectionString = //CONCATINATE YOUR FIELDS TOGETHER HERE
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
Sign up to request clarification or add additional context in comments.

Comments

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.