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:
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:
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):
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.