Sam Hobbs
posted
6490 posts
since
Sep 07, 2009
from
Los Angeles, California, USA
|
|
Re: c# select database based on combo box selection
|
|
|
|
|
|
|
|
|
|
|
Welcome to C# Corner.
The database is determined by the connection string or the database property of the connection. You do not specify how you are connecting to the database. It is frustrating to answer questions that do not specify inportant details because we can easily guess wrong what it is that they need and then the person asking the question says we are wrong. So please understand that if my answer is not relevant, it probably means you need to clarify womething.
You can build a connection string dynamically and change the database it specifies; there are various ways to do that. Or you can change the Database Property of the SqlConnection class if you are connecting to the database that way. If you are using a Typed Database (a TableAdapter or the Entity Framework) then you can probably modify the database property somehow.
Or you can have two separate connection strings. Or you could have two separate TableAdapters or Entity Framework tables. It depends on what you are using; what your organization is using.
|
|
|
|
|
Thinking is a feeling; pleasant for some and unpleasant for others.
|
|
|
|
|
|
Sharon Chapman
posted
4 posts
since
Jan 20, 2012
from
|
|
Re: c# select database based on combo box selection
|
|
|
|
|
|
|
|
|
|
|
Sam, This is how I solved the problem:
string cs = "";
if (comboBox1.SelectedIndex == 0) { cs = @"server=JITC-PC\GEOINT;database=Production_GEOINT;integrated security=SSPI"; } else if (comboBox1.SelectedIndex == 1) { cs = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI"; }
The button code looks like this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { }
Thanks, Sharon
|
|
|
|
|
|
Sam Hobbs
posted
6490 posts
since
Sep 07, 2009
from
Los Angeles, California, USA
|
|
Re: c# select database based on combo box selection
|
|
|
|
|
|
|
|
|
|
|
Very good, Sharon. There are very many ways to do it but as far as I know what you did will work well. If you need to improve it somehow then there are many alternatives.
|
|
|
|
|
Thinking is a feeling; pleasant for some and unpleasant for others.
|
|
|
|
|
|