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
291 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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