2

I am fairly new to c#. So far so good with learning. I do have one question, well maybe two.

How do you create a user dialog to allow the selection of a connection string when the project starts? I have 4 databases with the same schema and would like to allow the user to choose which to connect to when they launch the program.

I am currently using datasets but am willing to learn/try another method to acheive the desired result.

3 Answers 3

5

Put them into the config's connectionStrings area and provide the user with a ComboBox containing the names of the connection strings. Then use the one selected.

In your config:

<connectionStrings>
  <add name="Environment1" connectionString="connString1" providerName="System.Data.SqlClient" />
  <add name="Environment2" connectionString="connString2" providerName="System.Data.SqlClient" />
  <add name="Environment3" connectionString="connString3" providerName="System.Data.SqlClient" />
  <add name="Environment4" connectionString="connString4" providerName="System.Data.SqlClient" />
</connectionStrings>

In your code, add the connection strings to a ComboBox:

foreach (ConnectionStringSettings connString in ConfigurationManager.ConnectionStrings)
{
    myComboBox.Items.Add(connString.Name);
}

Get the name from the ComboBox, then get the connString you need and use it:

// Access chosen one:
string chosenName = (string)myComboBox.SelectedItem;
string connString = ConfigurationManager.ConnectionStrings[chosenName].ConnectionString;
Sign up to request clarification or add additional context in comments.

2 Comments

How would I code the DropDownList to select and then load the proper connection? My apologies for the newbie questions.
Since you're in WinForms, you want a ComboBox. Edited to reflect this.
1

You need to put the connection string for various database into the app.config or web.config files as suggested by zimdanen like:

 <connectionStrings>
    <add name="Database1" connectionString="Data Source=.\SQLEXPRESS;Initial 
     Catalog=db1;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Then you can use ConfigurationManager class to achieve the results:

foreach (ConnectionStringSettings connectionString in 
                     ConfigurationManager.ConnectionStrings)
 {
     //YOU CAN ADD NAME OF CONNECTION STRING TO A COMBOBOX FOR USER TO MAKE
     // A SELECTION
     //connectionString.Name
 }

Once the selection is made you can get the desired connection string with following line:

var s=ConfigurationManager.ConnectionStrings[<SELECTED NAME>].ConnectionString

5 Comments

So far so good. Just one final question, I think. The last block of code to get the desired connection, how would I use this to make sure the dataset uses the selected connection?
How are you filling your dataset? s here will contain the connection string as a string, so just pass that to wherever it's needed.
The dataset was generated by the wizard. I know, probably not the best way, but I'm learning. I see where the connection string is defined in the dataset.Designer.cs file for each TableAdapter. Where would I put the last block of code and how would I pass the string?
I really appreciate all the help. Still unsure how to pass the string. I was able to pass it using settings.settings to define it. When I run the app, I get an error "The ConnectionString property has not been initialized"
I haven't worked with wizard-based datasets. Try this?: stackoverflow.com/a/3478228/128217
0

Add Application Configuration File and add your connection strings in < connectionStrings>. In your code use ConfigurationManager.ConnectionString to enumerate through a list of connection strings.

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.