[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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 video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

872 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