Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
THE GIVEN CODE IS ONLY FOR SQL . BUT I WANT COMMAN FOR ALL GLOBAL DATA SOURCE(of any kind like my sql or excel)

What I have tried:

C#
using (SqlConnection con = new SqlConnection(strConnect))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", con))
                {
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        myComboBox.Items.Clear();
                        while (reader.Read())
                        {
                            myComboBox.Items.Add((string)reader["TABLE_NAME"]);
                        }
                    }
                }
Posted
Updated 13-Oct-17 0:07am
v3
Comments
GKP1992 13-Oct-17 6:06am    
If you know the connecion string, you know the DB provider. You can make query based on that information.

1 solution

There is not a simple and universal solution to your requirement, and you will need to implement something for every database type you want to handle.
A good start can be to create an interface which defines the method(s) that will return the data you want. For example:
C#
public interface IDbInformer {
   IEnumerable<string> GetTableNames();
}

Then you will need to create a class implementing the interface for every database type you need. For example:
C#
public class MsSqlInformer : IDbInformer {

   public IEnumerable<string> GetTableNames() {
      using (SqlConnection con = new SqlConnection(strConnect)) {
         con.Open();
         using (SqlCommand com = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", con))
         using (SqlDataReader reader = com.ExecuteReader()) {
            while (reader.Read()) {
               yield return (string)reader["TABLE_NAME"]);
            }
         }
      }
   }

}

Note that I did not mention the underlyng values you will need to include in you class (strConnect for example). It's up to you to decide how you want to handle them.

You then can use informer class to populate your combobox (or anything else):
C#
IDbInformer informer = new MsSqlInformer();
myComboBox.Items.Clear();
myComboBox.Items.AddRange(informer.GetTableNames());

You can define an informer class implementing IDbInformer interface for each and every data source type you want to be able to handle, and use them when appropriate. Providing an effective implementation for all of them exceeds by far the purpose of a quick answers forum, so I leave you with the necessary research which has to be done. I just provide you with one possible way to handle it, but I hope it will still be useful to you.
Kindly.
 
Share this answer
 
Comments
Richard Deeming 13-Oct-17 9:13am    
For SQL Server, it might be simpler to use the GetSchema[^] method to list the tables:
using (SqlConnection con = new SqlConnection(strConnect))
{
    con.Open();
    DataTable tables = con.GetSchema("Tables");
    foreach (DataRow row in tables.Rows)
    {
        yield return string.Format("[{0}].[{1}]", row["TABLE_SCHEMA"], row["TABLE_NAME"]);
    }
}
phil.o 13-Oct-17 10:03am    
I was not aware that there are now several methods to get these informations. Never had such a requirement to get the schema of a database, usually when I have to work with a database I already know its schema and which tables and columns to use. I'll try to stick it somewhere in my mind ; it may be useful some day. Thanks Richard for the update :)

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