How to get access to all column names of table in VB.Net/ADO.net


Using ‘getSchema’ method we can access database properties such as Column names, Tables name etc. ,with some tricky code in Visual Basic.Net . Here is how you can access all columns names in a Access table with OLEDB connection in VB ADO.net 2013.

Sub AddListViewColumsAuto( tbl As String)
Dim Company As OleDb.OleDbConnection
 Company = New OleDb.OleDbConnection
 Company.ConnectionString = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\.Net  Project\Accounting Pack\AccountingPack.accdb”
 Company.Open()
 Dim cmd As New OleDb.OleDbCommand(“Select * from ” & tbl, Company)
 Dim columns As String = “Columns”
 Dim dt As DataTable = Company.GetSchema(columns)
For Each row As DataRow In dt.Rows
 console.writeline(row.Item(“COLUMN_NAME”).ToString)
Next
Company.Close()
End Sub

I

First we create connection object, Company and a connection string. Then we need a OleDb.OleDbCommand object to set database to point a particular table, so that we created the cmd object and used it for query the input table.

II

In the next stage we created a column string in order to point Columns in the table. Using Getschema() method of connection object Company we stored each and every column name in the connection to the table variable dt.

III

In the final step we can access all rows from the dt which contain column names from table dt using COLUMN_NAME  field.

Here is the video tutorial of how you can access and add column names to the console.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.