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
266 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

16 Experts available now in Live!

Get 1:1 Help Now