我正在尝试传入包含几何多边形的 TVP 数据表。出于某种原因,我的几何对象在查询运行之前被拒绝,甚至出现以下错误:System.ArgumentException:不支持列“坐标”的类型。类型是“SqlGeometry”
我使用的 TVP 如下:
CREATE TYPE [prop].[ShapeTableType] AS TABLE(
[Coordinates] [geometry] NULL,
[Inclusive] [bit] NULL,
[Radius] [decimal](7, 2) NULL,
[ShapeType] [varchar](16) NULL
)
GO
填充它的代码如下:
创建数据表:
var dataTable = new DataTable();
dataTable.Columns.Add("Coordinates", typeof(SqlGeometry));
dataTable.Columns.Add("Inclusive", typeof(bool));
dataTable.Columns.Add("Radius", typeof(decimal));
dataTable.Columns.Add("ShapeType", typeof(string));
创建 SqlParameter:
var parameter = new SqlParameter(parameterName, SqlDbType.Structured)
{
TypeName = "prop.ShapeTableType",
Value = dataTable
};
_parameters.Add(parameter);
foreach (var shape in shapes)
{
var polygon = shape as Polygon;
if (polygon != null)
{
var geoPolygon = GetGeometryBuilder(polygon.Coordinates).ConstructedGeometry;
if (geoPolygon == null) continue;
dataTable.Rows.Add(geoPolygon, polygon.IsInclusive, DBNull.Value, "polygon");
}
}
从纬度/经度列表中提取地理对象的方法:
static SqlGeometryBuilder GetGeometryBuilder(IEnumerable<PointF> points)
{
SqlGeometryBuilder builder = new SqlGeometryBuilder();
builder.SetSrid(4326);
builder.BeginGeometry(OpenGisGeometryType.Polygon);
var firstPoint = points.First();
builder.BeginFigure(firstPoint.X, firstPoint.Y);
foreach (PointF point in points)
{
// check if any of the points equal the first point. If so, do not add them as we'll do this manually at the end
// to ensure the polygon is closed properly.
if (point != firstPoint)
{
builder.AddLine(point.X, point.Y);
}
}
builder.AddLine(firstPoint.X, firstPoint.Y);
builder.EndFigure();
builder.EndGeometry();
return builder;
}
我开始担心 DataTable 对象中不允许使用“SqlGeometry”类型,但我没有找到任何明确说明这一点的文档。