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.

Ashok
  • 1,868
  • 6
  • 36
  • 70
  • 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. –  Nov 28 '13 at 06: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](http://stackoverflow.com/questions/12769373/how-to-read-values-from-multiple-configuration-file-in-c-sharp-within-a-single-p), then you dont need a connection string to find your connection strings. – crthompson Nov 28 '13 at 07: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 – Ashok Nov 28 '13 at 07:05
  • @paqogomez I am connecting only two database at a time to each other. I don't want to hard code the connection string and want to give the user freedom to choose or input the connection string. Thankx – Ashok Nov 28 '13 at 07:07
  • 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. – crthompson Nov 28 '13 at 07:08
  • 1
    Perhaps the [Settings](http://msdn.microsoft.com/en-us/library/system.windows.interop.settings(v=vs.95).aspx) class would appeal to you more. – crthompson Nov 28 '13 at 07:14
  • @paqogomez can you illustrate how can we store and modify and use the update one connection string? thanks again. – Ashok Nov 28 '13 at 07:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42100/discussion-between-ashok-karale-and-paqogomez) – Ashok Nov 28 '13 at 07:44
  • @Ashok_Karale Perhaps you're off coding. Let me know if there is more I can help with. Good luck and please dont forget those upvotes and mark as answers. – crthompson Nov 28 '13 at 08:10

1 Answers1

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");
crthompson
  • 15,653
  • 6
  • 58
  • 80