Private Sub ReadData()
Dim vendorID As Integer
Dim productReader As SqlDataReader = Nothing
Dim vendorSQL As String = "SELECT VendorId, Name FROM Purchasing.Vendor"
Dim productSQL As String = "SELECT Production.Product.Name FROM Production.Product " + "INNER JOIN Purchasing.ProductVendor " + "ON Production.Product.ProductID = " + "Purchasing.ProductVendor.ProductID " + "WHERE Purchasing.ProductVendor.VendorID = @VendorId"
Dim connectionString As String = "Data Source=MSSQL1;" + "Initial Catalog=AdventureWorks;Integrated Security=SSPI" + "MultipleActiveResultSets=True"
Dim awConnection As New SqlConnection(connectionString)
Try
Dim vendorCmd As New SqlCommand(vendorSQL, awConnection)
Dim productCmd As New SqlCommand(productSQL, awConnection)
productCmd.Parameters.Add("@VendorId", SqlDbType.Int)
awConnection.Open()
Dim vendorReader As SqlDataReader = vendorCmd.ExecuteReader()
Try
While vendorReader.Read()
Console.WriteLine(vendorReader("Name"))
vendorID = CInt(vendorReader("VendorId"))
productCmd.Parameters("@VendorId").Value = vendorID
' The following line of code requires // a MARS-enabled connection.
productReader = productCmd.ExecuteReader()
Try
While productReader.Read()
Console.WriteLine((" " + productReader("Name").ToString()))
End While
Finally
productReader.Dispose()
End While
Finally
vendorReader.Dispose()
End Try
Console.WriteLine("Press any key to continue")
Console.ReadLine()
Finally
awConnection.Dispose()
End Try
End Sub 'ReadData