简体   繁体   中英

Read coordinates from geography SQL datatype to SqlDataReader in C#

I've searched around and I've seen you can access the latitude with myGeography.Lat but it doesn't seem to work when trying to read to sqldatareader , this is what I'm trying:

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"] 

SQL Geography is a data type that converts to Microsoft.SqlServer.Types.SqlGeography. Assuming myGeography is a geography, you can do it this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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