Question list field name of a table?

patrick

Member
Joined
Aug 12, 2008
Messages
7
Programming Experience
3-5
How can we display only the field name of a table into a combo box control
 
Last edited by a moderator:
If you're saying you want to list all the tables a database contains in a ComboBox then you should create the appropriate ADO.NET connection object and call its GetSchema method. That will return a DataTable that you can bind to your ComboBox. Here's an example for SQL Server.
VB.NET:
Using connection As New SqlConnection("connection string here")
    connection.Open()
    myComboBox.DisplayMember = "TABLE_NAME"
    myComboBox.DataSource = connection.GetSchema("TABLES")
End Using
Things will be different to varying degrees for other databases.
 
Ah, I think I might have misread the question slightly. I think now maybe you want to list the columns in a table, not the tables in a database. In that case it's much the same but you'd specify COLUMNS as the collection name. You'll also want to specify a restriction of a particular table. Exactly how that's done may depend on the database but each database will provide you what you need to know. If you call GetSchema with no arguments you'll get all the available collections. One of those will likely be named RESTRICTIONS and tell you what restrictions you can apply to each collection. In the case of COLUMNS, I believe that for SQL Server and Access the TABLE_NAME restriction is the third, so you'd call GetSchema like this:
VB.NET:
myComboBox.DataSource = connection.GetSchema("COLUMNS", New String() {Nothing, Nothing, "MyTable"})
 

Latest posts

Back
Top