Click here to Skip to main content
15,511,131 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some help, I have my app MKS, created a database MKS_2017 and using it. It works fine. Now I created MKS_2018 and using it an it also working fine.

I need to implement when login in my app MKS using (username,password) and in combox to choose database to show data in my app (MKS_2017 or MKS_2018)

What I have tried:

I read database from server in this way

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

   using (SqlConnection con = new SqlConnection(cs))
   {
       con.Open();
       // 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 'MKS%'", con))
       {
             using (IDataReader dr = cmd.ExecuteReader())
             {
                        while (dr.Read())
                        {
                            list.Add(dr[0].ToString());
                        }
                    }
              }
            }
            return list;

        }
Posted
Updated 20-Aug-18 22:20pm
v3
Comments
Manish K. Agarwal 21-Aug-18 3:01am    
Not able to really get what is the problem in this case. It should be straight, just based on the selection from drop down, change the connection parameter.
Goran Bibic 21-Aug-18 3:09am    
Recomandition? Some help?
Goran Bibic 21-Aug-18 3:11am    
I have app, need to use data from datababe mks_2017 (for 2017. year) and from database mks_2018 (data from 2018. year). When login to chose from combobox database I want to use
Manish K. Agarwal 21-Aug-18 3:14am    
First of all no need to manage 2 DBs based on year. Anyway even if you have made it, the way you are creating SqlConnection(cs) must be specifying MKS_2017 or MKS_2018, now based on the user choice in drop down you have specify it in "cs"

You have a list of databases, all you have to do is use the selected name as a part of your connections string.

So set a static connection string in your app:
C#
public static strConnect = "";
and you can access that when you need it:
C#
using (SqlConnection con = new SqlConnection(MyClassWhereIPutIt.strConnect))
   {
   ...
   }
Then all you need to do is construct the connection string. There are two ways to do this:
1) If you databases all use the same user and password details, it's simple: construct a "basic" connection string with a placeholder for the DB name:
C#
const string basic = @"Server=[server_name]; Database={0}; password=[password]; user = [user_name]";
And use Replace to set the name:
C#
strConnect = basic.Replace("{0}, nameOfTable);

2) If they don't, then you will need to maintain a valid connections string list somewhere (I'd suggest in a table in a "master" DB in SQL which lets you fetch a working connection string from SQL using the required DB name in the WHERE clause.)
 
Share this answer
 
Below code will support two databases via we will fetch the records.
select top 5* from QA.dbo.user_userdetail as I1 inner join [MOCRollOver2].dbo.user_userdetail I2 on I1.User_ID=I2.User_ID

Fetching the records are below displayed.
User_ID	UserName	Password
1  	Superdev  1000:F0
2	Admin	  1000:wLVgI5
3	Import    1000:w5
 
Share this answer
 
v3

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