1

我正在使用 Visual Studio 2010 和 vb.net 创建一个 .asmx 网络服务,并使用 SQL Server 2008,但下面的查询提供了数据库中的所有地址。我需要将下面的查询修改为返回十个最近位置的空间查询。Lat 和 Long 是小数,但数据库中有一个名为 [Location] 的地理列。帮助!

<WebMethod()> _
Public Function GetFuelStops(ByVal Lat as Double, ByVal Long as Double) As FuelStop()
    Dim resultList = New List(Of FuelStop)()

    Using sqlCon As New SqlConnection()
    sqlCon.ConnectionString = "Data Source=(local);Initial Catalog=Example_DB;User ID=Turd;Password=Fergison"
    Dim sql = <sql>
        SELECT
            [Physical_Address_Street]
            , [Physical_Address_Local]
            , [Physical_Address_State]
            , [Physical_Address_Zip]
            , [Lat]
            , [Long]
            , [Phone_Number]
        FROM Gas_Stations
        WHERE Location_Type = 1
        </sql>

    Dim command As New SqlCommand()
    command.CommandText = CStr(sql)
    command.Connection = sqlCon
    sqlCon.Open()

    Using reader = command.ExecuteReader()
        While reader.Read()
            Dim fuelStop = New FuelStop()
            fuelStop.Physical_Address_Street = reader.GetString(0)
            fuelStop.Physical_Address_Local = reader.GetString(1)
            fuelStop.Physical_Address_State = reader.GetString(2)
            fuelStop.Physical_Address_Zip = reader.GetString(3)
            fuelStop.Phone_Number = reader.GetString(4)

            resultList.Add(fuelStop)
        End While
       End Using
    End Using
Return resultList.Skip(skip).Take(take).ToArray()

End Function
4

1 回答 1

2

我强烈建议您仔细研究 SQL Server 中的地理空间功能,而不是寻求实际的解决方案。这是一项强大的功能,但如果以错误的方式使用,可能会降低性能。

以下查询应该做你想做的事:

DECLARE @center GEOGRAPHY

SET @center = geography::Point(46.969345, 8.592703, 4326)

SELECT TOP 10
    [Physical_Address_Street]
    , [Physical_Address_Local]
    , [Physical_Address_State]
    , [Physical_Address_Zip]
    , [Lat]
    , [Long]
    , [Phone_Number]
FROM Gas_Stations
WHERE Location_Type = 1
ORDER BY @center.STDistance(Location) ASC
于 2013-04-24T22:24:03.953 回答