Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# geography column in datatable

I have a datatable in C#, and I want to add a column to store latitude and longitude coordinates in a geography format to bulkcopy in SQL Server after that.

In what format should I create the datacolumn for this?

like image 516
George Avatar asked Nov 29 '25 10:11

George


1 Answers

We have to use the reference DLL that is located under "C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Types.dll":

using Microsoft.SqlServer.Types;

After that, we can create the column in datatable, store some data and successfully send them to SQL Server via bulkcopy.

DataTable dataTable = new DataTable();
dataTable.Columns.Add("Geom", typeof(SqlGeometry));

DataRow newRow = datatable.NewRow();
newRow["Geom"] = SqlGeometry.Point(lat, lon, 4326);

datatable.Rows.Add(newRow);

SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connection);
sqlBulkCopy.DestinationTableName = "MySpatialDataTable";
sqlBulkCopy.WriteToServer(dataTable);
like image 120
George Avatar answered Dec 01 '25 00:12

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!