简体   繁体   English

从 SQL 中检索单条数据并在 ASP.NET 中显示

[英]Retrieve single piece of data from SQL and display in ASP.NET

I'm new to coding in general, and am working on a social networking project in ASP.NET.我一般不熟悉编码,并且正在 ASP.NET 中从事社交网络项目。 I have to come up with a way to display nearby users based on the location they have set in their profile.我必须想出一种方法来根据他们在个人资料中设置的位置来显示附近的用户。 It involves retrieving that piece of data (their set location) from SQLServer, so I can display relevant users.它涉及从 SQLServer 检索那条数据(它们的设置位置),以便我可以显示相关用户。

How would I go about doing this?我该怎么做呢? I am using C# as the controller, and need to reference it in an html document.我使用 C# 作为控制器,需要在 html 文档中引用它。 I appreciate any help I can get.我很感激我能得到的任何帮助。

Thanks!!!谢谢!!!

Bit if a broad topic, so if I may suggest that you start with the database and worry about the UI later.如果是一个广泛的主题,那么我是否建议您从数据库开始,然后再担心 UI。

Let's assume you have a table of users, and their current locations are logged into the database.假设您有一个用户表,并且他们的当前位置已登录到数据库中。

在此处输入图片说明

Now, you want to find all within a 5 mile radius.现在,您要查找 5 英里范围内的所有内容。 Consider the following.考虑以下。 Note @Users is just a table variable and would be replaced with your actual table.注意 @Users 只是一个表变量,将替换为您的实际表。

Declare @Users table (ID int,UserName varchar(25),Lat float,Lng float)
Insert Into @Users values
(1,'James',41.721913,-71.265238),
(2,'Joseph',41.709084,-71.386673),
(3,'Mary',41.709084,-71.386673),
(4,'Sam',41.68745,-71.270122),
(5,'Sally',41.756903,-71.222215),
(6,'John',41.744068,-71.315024)   -- Let's assume this is my current position


Declare @MyLat float = 41.744068
Declare @MyLng float = -71.315024
Declare @Miles float = 5

Select MilesAway  = [dbo].[udf-Geo-Calc-Miles] (@MyLat,@MyLng,A.Lat,A.Lng)
      ,A.*
 From @Users A
 Where [dbo].[udf-Geo-Calc-Miles] (@MyLat,@MyLng,A.Lat,A.Lng) <= @Miles
 Order by 1

Returns退货

在此处输入图片说明

Now, I use a UDF to calculate the miles (can be converted to meters)现在,我使用 UDF 来计算英里(可以转换为米)

CREATE Function [dbo].[udf-geo-Calc-Miles] (@Lat1 float,@Lng1 float,@Lat2 Float,@Lng2 float)  
Returns Float as  
Begin 
   Declare @Miles Float = (Sin(Radians(@Lat1)) * Sin(Radians(@Lat2))) + (Cos(Radians(@Lat1)) * Cos(Radians(@Lat2)) * Cos(Radians(@Lng2) - Radians(@Lng1)))
   Return Case When @Miles is null then 0 else abs((3958.75 * Atan(Sqrt(1 - power(@Miles, 2)) / @Miles))) end
End

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM