mercredi 21 février 2018

populate dropdownlist with DBnames except system DBs

I need to connect to SQL db from C# windows application and populate dropdown list from connection to have all db from server in dropdown except system DBs I have this code

 using (SqlConnection thisConnection = new SqlConnection("Server=" + serverName + ";User Id=" + uName + ";Password=" + pass + ";"))
                {
                    thisConnection.Open();

                    // Assign it to dropdown menu
                    DataTable databases = thisConnection.GetSchema("Databases");
                    foreach (DataRow database in databases.Rows)
                     {
                         String databaseName = database.Field<String>("database_name");

                        //  MessageBox.Show(databaseName);
                          if (databaseName  == "master")
                          {

                          }
                        if (databaseName == "model")
                          {

                        }
                        if (databaseName == "tempdb")
                        {

                        }
                        if (databaseName == "msdb")
                        {

                        }
                        else
                          {
                            dd_ddase.Items.Add(databaseName);
                        }


                    }

However it is still return me system dbs in drop down, what I am doing wrong ? thanx

Aucun commentaire:

Enregistrer un commentaire