Search code examples
.netsqlvb.netado.netindexed-properties

What is the meaning of this code?


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)?


Solution

  • 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.