C# - SqlServerTypes

  • 125
  • 0

紀錄運用SqlServerTypes這個套件來做空間運算

安裝 SqlServerTypes 完畢之後,在使用SqlServerTypes的時候要注意他的說明文件

在一般網站的後台或是排程的程式按照說明文件加上都可以正常運作

補充 Web API

在Web API的controller內沒有Serve.MapPath可以使用

要改為

SqlServerTypes.Utilities.LoadNativeAssemblies(System.Web.Hosting.HostingEnvironment.MapPath("~/bin"));

 =====

空間運算

在sql sever 裡面開資料表欄位的時候,資料型態可設為 geography

要新增這一種類型的資料,新增方式如下

Location = System.Data.Entity.Spatial.DbGeography.PointFromText($"POINT ({longitude} {latitude})", 4326)

計算兩點之間的距離

//轉換要查詢的座標
System.Data.Entity.Spatial.DbGeography searchLocation 
= System.Data.Entity.Spatial.DbGeography.PointFromText($"POINT ({longitude} {latitude})", 4326);
// 使用 Distance 來計算兩點之間的距離
var parkingSpots = db.ParkingDatas.Where(x => searchLocation.Distance(x.Location) < radius).ToList();