dotMorten / Microsoft.SqlServer.Types

a .NET Standard implementation of the spatial types in `Microsoft.SqlServer.Types`
Apache License 2.0
75 stars 33 forks source link

Insert to SQL Server throws exception - `Specified type is not registered on the target server`. #77

Open valdisiljuconoks opened 2 years ago

valdisiljuconoks commented 2 years ago

Hi,

I'm not sure about purpose of the library now. Is it meant to be able to work with spatial types or I can use it also to fill in tables in SQL server (2019)?

Using data tables I got follownig error:

[11:16:29 ERR] Error storing timetable dataset
System.InvalidOperationException: The given value of type SqlGeography from the data source cannot be converted to type udt of the specified target column.
 ---> System.ArgumentException: Specified type is not registered on the target server. Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types, Version=2.5.0.0, Culture=neutral, PublicKeyToken=null.
   at System.Data.SqlClient.SqlConnection.GetInfoFromType(Type t)
   at System.Data.SqlClient.SqlConnection.GetBytes(Object o, Format& format, Int32& maxSize)
   at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
   at System.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col)
   at System.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
   at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
   at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)

Data table code:

table.Columns.Add(nameof(FlexibleAreaEntity.PosList), typeof(SqlGeography));

..

newRow[nameof(FlexibleAreaEntity.PosList)] =
    SqlGeography.STGeomFromText(new SqlChars($@"MULTIPOLYGON ((({_separator.Separate(item.PosList)})))"), 4326);
thaianhduc commented 10 months ago

@valdisiljuconoks I had the same problem. I changed to use Microsoft.Data.SqlClient instead of System.Data.SqlClient. It works.