Solved

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

Posted on 2007-11-21
4
817 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

910 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

24 Experts available now in Live!

Get 1:1 Help Now