请看下面的代码:
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Form1
Private _ConString As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objDR As SqlDataReader
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Dim id As Integer
Try
_ConString = ConfigurationManager.ConnectionStrings("TestConnection").ToString
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand("SELECT Person.URN, Car.URN FROM Person INNER JOIN Car ON Person.URN = Car.URN AND PersonID=1")
objCommand.Connection = objCon
objCon.Open()
objDR = objCommand.ExecuteReader(ConnectionState.Closed)
Do While objDR.Read
id = objDR("URN") 'line 19
Loop
objDR.Close()
Catch ex As Exception
Throw
Finally
End Try
End Sub
End Class
请参阅第 19 行。是否可以执行类似的操作:
objDR("Person.URN")
当我这样做时: objDR("URN") 它返回 CarURN 而不是 Person URN。我意识到一种解决方案是使用 SQL AS 关键字,即:
SELECT Person.URN As PersonURN, Car.URN AS CarURN FROM Person INNER JOIN Car ON Person.URN = Car.URN AND PersonID=1
进而:objDR("PersonURN")
但是,由于应用程序的设计方式,我想尽可能避免这种情况,即它会涉及大量的硬编码。