Solved

count of items or colums of a SQL query using data reader

Posted on 2007-11-21
4
803 Views
Last Modified: 2011-09-20
Greetings,

I need to count the number of colums in a result of a sql query that uses datareader to read the results:
here is the vb code:

 Dim conn As New SqlConnection(connectionString)
        Dim command As New SqlCommand(strVFD_MO, conn)
        command.CommandType = CommandType.Text

        conn.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
        Dim i As Integer = 0
        If reader.HasRows Then
            Do While reader.Read()
' the number of colums is unknown, so item id such as reader(0) should not be hard coded
                Debug.WriteLine(reader(0), reader(1))
            Loop
        End If

thanks!!
0
Comment
Question by:exquisite_uae
  • 3
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 125 total points
ID: 20326410

 Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _

          "integrated security=sspi;database=MyDatabase")
 

      'Sql Query 1

      Dim sql As String ="SELECT  FirstName, LastName FROM Employee;  "
 

      'Create Command object

      Dim thisCommand As New SqlCommand _

         (sql, thisConnection)
 

      Try

         ' Open Connection

         thisConnection.Open()

         Console.WriteLine("Connection Opened")
 

         ' Execute Query

         Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
 
 

         ' Get number of columns

         Console.WriteLine()

         Console.WriteLine("Number of columns in a row: {0}", _

            thisReader.FieldCount)
 
 

         'Close DataReader

         thisReader.Close()
 

      

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326412
Use  thisReader.FieldCount
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326421
FieldCount - returns the number of columns in the result set.
GetFieldType(int) - returns the Type of the column.
GetDataTypeName(int) - returns the Name of the back-end database column
type.
GetName(int) - returns the Column Name.
0
 

Author Closing Comment

by:exquisite_uae
ID: 31410320
thanks..thats exactly what i need :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

20 Experts available now in Live!

Get 1:1 Help Now