VB.net Count Columns in an Access table

Hi

What VB.net code would I use to count the number of columns in an Access table?

Thanks
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
mbizupCommented:
For an Access table from VB.Net, I believe you would have to use a method such as filling a dataadapter/datareader/etc and getting the column count.

This is using a datareader.  You'd need Imports System.Data.SQLClient at the top of your code module, and I'm assuming that you have already defined your connection (cn in the code below):


    Using cn
        Dim cmd As SqlCommand = New SqlCommand( "SELECT * FROM YourTable", cn)
        cn.Open()

        Dim dr As SqlDataReader = cmd.ExecuteReader()
        Me.txtSomeTextbox.Text = dr.FieldCount

        dr.Close
        cn.Close

    end Using

Open in new window

0
 
Rgonzo1971Commented:
Hi,

pls try

myRst.Fields.Count 

Open in new window

Regards
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

I connect to the database via a connection string so am looking for SQL code to do this
0
 
Rgonzo1971Commented:
Hi,

pls try
SELECT COUNT(*) AS ColumnCount
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename';

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.