Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't help for SqlDataAdapter.Fill #28

Open
mxcolin opened this issue Aug 29, 2022 · 5 comments
Open

Doesn't help for SqlDataAdapter.Fill #28

mxcolin opened this issue Aug 29, 2022 · 5 comments

Comments

@mxcolin
Copy link

mxcolin commented Aug 29, 2022

This library works for a narrow case where you don't need to use GetFieldType of the DataReader or if you don't need to Fill a DataTable from an adapter. In this case you get null references and failures.

@mxcolin
Copy link
Author

mxcolin commented Aug 30, 2022

Given there is little to no documentation on this I've tried my best to get this to work, but take a look at the following:

SqlConnection connection = new SqlConnection("...");
connection.Open();

// Spatial Table with 3rd column as Geometry
string sql = "SELECT * FROM [spatial_table]";

SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader dataReader = command.ExecuteReader();

// This will return null, possible to get around this but not great
var gType = dataReader.GetFieldType(2);

dataReader.Close();

SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();

// This will throw a System.InvalidOperationException: 'DataReader.GetFieldType(2) returned null.' exception
adapter.Fill(dataTable);

So, yeah this works if you want to blindly get a geometry column that you already know is there and you don't want to use a DataAdapter to put it into a DataTable. In that case you are fine.

@bricelam
Copy link
Collaborator

bricelam commented Aug 30, 2022

Correct. This library was created as a workaround to dotnet/SqlClient#30. It bypasses the expected SqlGeography and SqlGeometry types and reads and writes the underlying bytes directly using NetTopologySuite types instead.

Any code other than the examples provided in the README will still try to go through the (non-existent) SqlGeography and SqlGeometry types and throw.

Please upvote dotnet/SqlClient#30 so the SqlClient team at Microsoft can prioritize full spatial support in modern .NET.

@bricelam
Copy link
Collaborator

Another workaround available is the dotmorten.Microsoft.SqlServer.Types NuGet package. I believe it should be able to work with SqlDataAdapter.

@bricelam
Copy link
Collaborator

And finally, you might even be able to just install the official .NET Framework NuGet package into a .NET project. I expect it'll work just fine on Windows.

@mxcolin
Copy link
Author

mxcolin commented Aug 30, 2022

The dotmorten package throws exceptions and has some strange behavior, rather than go down that route I've just built the DataTable using a DataReader and this library. It's a decent stop gap but yeah ideally Microsoft would provide actual support. Thanks for the responses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants