Link to home
Start Free TrialLog in
Avatar of exquisite_uae
exquisite_uae

asked on

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

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!!
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use  thisReader.FieldCount
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.
Avatar of exquisite_uae
exquisite_uae

ASKER

thanks..thats exactly what i need :)