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
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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