Goran Bibic

Goran Bibic

  • 455
  • 2.9k
  • 198k

Select values from diferent db with same app C#

Sep 4 2018 12:26 PM
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?
 
 
Combobox value are find on next way
 
  1. InitializeComponent();  
  2. comboBox1.DataSource = GetDatabaseList(); 
 
  1. public List<string> GetDatabaseList()  
  2.         {  
  3.             List<string> list = new List<string>();  
  4.   
  5.             using (SqlConnection con = new SqlConnection(cs))  
  6.             {  
  7.                 con.Open();  
  8.   
  9.                 // Set up a command with the given query and associate  
  10.                 // this with the current connection.  
  11.                 using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases where name like '20%'", con))  
  12.                 {  
  13.                     using (IDataReader dr = cmd.ExecuteReader())  
  14.                     {  
  15.                         while (dr.Read())  
  16.                         {  
  17.                             list.Add(dr[0].ToString());  
  18.                         }  
  19.                     }  
  20.                 }  
  21.             }  
  22.             return list;  
  23.   
  24.         } 
 

Answers (4)