Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
326 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 60

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 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 24756653
0
Independent Software Vendors: 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!

 

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 60

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 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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