DataTable中的SqlGeometry参数错误转换为TVP

时间:2014-09-08 19:43:56

标签: c# sql parameters datatable sqlgeometry

我正在尝试传入包含几何多边形的TVP数据表。出于某种原因,在查询甚至运行之前,我的几何对象被拒绝,并出现以下错误:System.ArgumentException:不支持“Coordinates”列的类型。类型是'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'类型,但我没有找到任何明确说明这一点的文档。

0 个答案:

没有答案