从地理SQL数据类型读取坐标到C#中的SqlDataReader

时间:2018-01-10 01:53:42

标签: c# sql sqldatareader geography reader

我已经四处搜索了,我看到你可以使用myGeography.Lat访问纬度,但在尝试阅读sqldatareader时它似乎不起作用,这就是我正在尝试:

String sql = "SELECT * FROM myTable WHERE myGeography.STDistance(geography::Point(somelatitude, somelongitude, 4326)) <somerange 

SqlDataReader reader = command.ExecuteReader()

int lat,long    
lat = reader["myGeography.Lat"]    
long = reader["myGeography.Long"] 

1 个答案:

答案 0 :(得分:1)

SQL Geography是一种转换为Microsoft.SqlServer.Types.SqlGeography的数据类型。假设myGeography是一个地理位置,你可以这样做:

Microsoft.SqlServer.Types.SqlGeography geo = (Microsoft.SqlServer.Types.SqlGeography)reader.GetValue["myGeography"];
int long = geo.Long;
int lat = geo.Lat;