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

x
?
Solved

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

Posted on 2007-11-21
4
Medium Priority
?
897 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

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!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

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