I'm trying to save a series of SqlGeometry
values to a SQL Server 2008 database.
Basically I have a tabletype in a SQL Server stored procedure which looks like this:
CREATE TYPE [dbo].[TableType_Example] AS TABLE
(
[SpatialID] [bigint] NOT NULL,
[RecordID] [bigint] NOT NULL,
[geom] [geometry] NOT NULL
)
I then build a datatable in C# and send it like this:
public static bool SaveSpatialDataElements(long recordID, List<BOSpatial> featureList)
{
//Setup features datatable
DataTable dtFeatures = new DataTable();
dtFeatures.Columns.Add("SpatialID", typeof(SqlInt64));
dtFeatures.Columns.Add("RecordID", typeof(SqlInt64));
dtFeatures.Columns.Add("geom", typeof(SqlGeometry));
foreach(var curFeature in featureList)
{
object[] curRowObjects = new object[dtFeatures.Columns.Count];
curRowObjects[0] = curFeature.SpatialID;
curRowObjects[1] = recordID;
using (var reader = new StringReader(curFeature.ToGML()))
{
using (var xmlreader = new XmlTextReader(reader))
{
curRowObjects[2] = SqlGeometry.GeomFromGml(new SqlXml(xmlreader), 0);
}
}
DataRow row = dtFeatures.NewRow();
row.ItemArray = curRowObjects;
dtFeatures.Rows.Add(row);
}
DbConn conn = new DbConn();
conn.Connect();
conn.ExecuteStoredProcedure(false, "USP_tblSpatialLocation_Update", recordID, dtFeatures);
conn.Disconnect();
return true;
}
This works fine for all my other datatables but this one contains a SqlGeometry
column and it falls over with the error message:
An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code
Additional information: The type of column 'geom' is not supported. The type is 'SqlGeometry'
This doesn't make any sense to me as that datatype seems supported by what I'm reading in the documentation.
Any thoughts?
Edit:
The comments below and the article I've linked: https://viswaug.wordpress.com/2008/09/29/inserting-spatial-data-in-sql-server-2008/ seem to suggest that I need to change the datattype of SqlGeometry
to SqlDbType.Udt
. Sadly as I'm using a datatable I've got no way of defining UdtTypeName = “GEOMETRY”;
as this is set on the parameter.
SqlGeometry
is. All parameters to SQL have to be types SQL knows how to handle. – AutoerotismExecuteStoredProcedure
with the datatable? – However