?
Solved

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

Posted on 2007-11-21
4
Medium Priority
?
878 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
[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
  • 3
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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