Solved

how can I get the number of columns in a table, how can I get the column names vb.net sql server ce

Posted on 2009-07-01
8
276 Views
Last Modified: 2012-06-21
I keep adding columns to tables as I develop however I still want to interact with old tables with less columns
how can I get the number of columns in a table, how can I get the column names
vb.net sqlserverce
0
Comment
Question by:Dov_B
  • 4
  • 4
8 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24756564
If you are already retrieving the data from the table using a DataReader for example with SELECT * FROM your_table, then you can use FieldCount to get the count of columns and GetName(x) to get the name of column at index x.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader_members.aspx

If you are in your SQL server itself, you can use the information schema or syscolumns data to determine the number of columns for a given table without having to select any of the data from the table.
0
 

Author Comment

by:Dov_B
ID: 24756623
I am in vb.net 2008 express
intellisense is not giving me fieldcount when I type
myDataset.Tables(0).fieldcount
nor can I seem to find the get name method
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24756653
0
 

Author Comment

by:Dov_B
ID: 24756777
oh I never used the data reader can you tell me how to use it?
here is the code I use for dataset

Dim constringN = "Data Source=" & "c:\Auto\subjects\" & TbFileName.Text & "\" & TbFileName.Text & ".sdf;Persist Security Info=False;"

        Dim myCon As New SqlCeConnection(constringN)

        pvDa = New SqlCeDataAdapter(mSql, myCon)

        myCon.Open()

        ' Create and fill dataset

        pvDs = New DataSet()

        On Error GoTo exitme

        pvDa.Fill(pvDs, mTable)
 

        myCon.Close()

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24756860
You don't need to, I was just pointing out that FieldCount was meant for that.  You can use the DataAdapter like you are to fill the DataSet and then use:

pvDs.Tables(0).Columns.Count
0
 

Author Comment

by:Dov_B
ID: 24757003
what about the names of the columns?
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 24757085
Once you have the Columns collection, you can loop through and get the name.

Just as a test, you can use this:
pvDs.Tables(0).Columns.Item(0).ColumnName

But for more practical use, you will probably want to do something like this:

For Each c As DataColumn In pvDs.Tables(0).Columns
            c.ColumnName 'Get column name and do something with it
        Next
0
 

Author Closing Comment

by:Dov_B
ID: 31598870
I am asking a related question if you get a chance
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now