0

Here is the code sample provided in .Net Sdk:

Private Sub ReadOrderData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM dbo.Orders;"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data.
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using
End Sub

My question: Is that this reader(0) actually the shortcut for calling this property reader.item(0)?

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
user774411
  • 1,749
  • 6
  • 28
  • 47

2 Answers2

5

reader(0) refers to the very first field in the retrieved row. However you better reference field by name, OrderID in this case.

Take a look at SqlDataReader.Item Property.

This is indexed property. You can find some details here.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • Yes, I know the purpose, but when creating our own class, we need to add our methods and properties, right? In this case, a call to reader(0) actually a call to which sub, function, or property for the SqlDataReader class? – user774411 Jun 06 '11 at 04:32
  • @Dee Jay - SqlDataReader.Item. There is a reference in the answer. – Alex Aza Jun 06 '11 at 04:33
  • You mean Item property of the SqlDataReader class, right? Ok, if I were creating my own class and wish to have same behaviour like this SqlDataReader class, can I just declare the item property and the call the instance of my class just like current reader(0) call? Do I need to add anything (implement or inherit something)? – user774411 Jun 06 '11 at 04:38
  • @Dee Jay - you are looking for 'indexed property'. I added a reference to MSDN to the answer. – Alex Aza Jun 06 '11 at 04:39
2

reader(0) , reader (1) and so on refers to columns/ fields of the row u r retriving,

reader(0)---> column 1

reader(1)---> column 2 ...

You can use direct field names in place of these 'readers'. But in books/tutorials, code is always written like that.

Stuti
  • 1,620
  • 1
  • 16
  • 33