Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some simple app

Two database:
1) 2017 (table name partners(id, name))
2) 2018 (table name partners(id, name))

On Form 1 have username, password, button login and combo box.
On Form 2 empty partnerdatagrid

I need when in combobox chose value 2017 show data from database 2017 (table name partners(id, name)),
if choose value 2018 to show data from
2018 (table name partners(id, name))

Some help?

What I have tried:

Combobox value are find on next way

     comboBox1.DataSource = GetDatabaseList();

public List<string> GetDatabaseList()
            List<string> list = new List<string>();

            using (SqlConnection con = new SqlConnection(cs))

                // Set up a command with the given query and associate
                // this with the current connection.
                using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases where name like '20%'", con))
                    using (IDataReader dr = cmd.ExecuteReader())
                        while (dr.Read())
            return list;

Updated 5-Sep-18 0:16am
MadMyche 4-Sep-18 13:30pm    
Are these databases on the same server? Are there multiple connection strings involved? Can the same credentials access both DBs? What flavor of DB server are you using?
Goran Bibic 4-Sep-18 13:36pm    
Same server, Yes, Yes(but if there is a better solution suggest),Microsoft SQL 2016.
ZurdoDev 4-Sep-18 13:44pm    
Just change what database you connect to in the ConnectionString. Where are you stuck?
Goran Bibic 4-Sep-18 13:48pm    
I want to log in to the application, on Form1 to select the combobox(2017 or 2018)and to use that conn string

Some help?
Goran Bibic 4-Sep-18 13:48pm    
When choose 2017 to use that conn string, when chose 2018 to use that conn string

General note:
Stop "monkey-programming" and start thinking!

First of all...
An idea to store partners (keep partners details) into two different databases is bad idea!, because of...
a) you loose control over your database for set of reason (new partner id, etc.),
b) (if not `a` then...) you need to duplicate partners details every year in new database.

Second of all...
If you need to copy data between two different databases placed on the same server, you don't need to change connection string, because you already are connected to this server. You need to change SELECT statement:
SELECT <<ListOfFields>>
FROM Database2.Partners;

Note: you have to have permission to do that!
If you need to copy data between two different databases placed on two different servers, you can use Linked Servers (Database Engine)[^] to be able to access data from outside sql server.

Please, read about DAL[^] and BLL[^].
Share this answer
Just make "2" connections; each with their own connection string.

It takes time to "connect"; open both at startup; pass / take the one you need whenever.

Add a "close / dispose" method at a key point / interval when the connections are no longer needed.

SqlConnection _con1 ...
SqlConnection _con2 ...

List<sqlconnection> _connections = new List<...>() { _con1, _con2 };

Share this answer
Goran Bibic 4-Sep-18 14:04pm    
That will be for all app when login?
Goran Bibic 4-Sep-18 14:32pm    
I choose one conection when login. Hot to apply for all app that conn string when login whit him?
Gerry Schmitz 4-Sep-18 14:45pm    
It is obvious from your rapid responses that you are NOT taking the time to properly consider what you are being told.

Don't expect "more".
Not positive on your question here. First, I'll make a few assumptions...1) you are using WinForm 2) for the sake of example, the combo box that presents the choices 2017 and 2018 is named comboBox2.

In that case, after your login is complete, I'd do the following. Select a connection string based on the value of comboBox2.SelectedIndex. For example:
string cs = comoBox.SelectedIndex == 1 ? connectionString2018 : connectionString2017;

Then, I would set the data source for your other combo box as follows (passing cs as a parameter):
comboBox1.DataSource = GetDatabaseList(cs);

Alternatively, if the 2017/2018 combo box appears elsewhere, you could also subscribe to the SelectedIndexChanged event and initialize the DataSource (as described previously) in that event.

I notice now that I have misread your question and that your GetDatabaseList function is actually getting the list of database names, to populate your database selection list.

If you really only have the possibility of 2017 and 2018, hard code them in the combo box and select a connection string as described above.

If, instead, you need to support a random number of databases, you can populate the database list as you currently do and subscribe to the SelectedIndexChanged event to (somehow) get a connection string for that database.

I would consider this a poor design. If you have a bit more flexibility, consider moving to a single database that is not year-specific. Then, add a new table PartnersByYear that has a year column and a partnerId column.

Then, with a fairly basic INNER JOIN, between partnersByYear and partners you can select the partners for the year that they participate, using a combo box to get the year in the WHERE clause. This would be a lot less cumbersome than maintaining a bunch of separate databases.
Share this answer
Goran Bibic 4-Sep-18 14:22pm    
string cs = comoBox.SelectedIndex == 1 ? connectionString2018 : connectionString2017;

Where to put this?

namespace test_2017_2018
public partial class Form1 : Form

//string cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=2017;Integrated Security=True";
string cs = comboBox1.SelectedIndex == 1 ? test_2017_2018.Properties.Settings.TEST_2018 : test_2017_2018.Properties.Settings.TEST_2017;
public Form1()
comboBox1.DataSource = GetDatabaseList(cs);
Eric Lynch 4-Sep-18 15:47pm    
It looks like you may have replied about the same time I updated. I'd take a look at the update and consider, if possible, consolidating databases. This is the better solution. That way you don't have to maintain multiple databases, connection strings, etc. An approach is suggested in the update.

If you decide to limit to two databases as above, in response to "where to put this?" The correct question is not "where" but "when". The answer is in the original response: "after your login is complete". You must have some code/event that validates your login. You would put it immediately after validation is completed.
Goran Bibic 4-Sep-18 15:54pm    
Solved...thanks for help
Eric Lynch 4-Sep-18 15:55pm    
no problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900