SQL Server Geometry Type action Note

Source: Internet
Author: User

IF object_id (' dbo. Spatialtable ', ' U ') is not NULL
DROP TABLE dbo. spatialtable;
GO

CREATE TABLE spatialtable
(id int IDENTITY (),
GeomCol1 geometry,
GeomCol2 as Geomcol1.stastext ());
GO

INSERT into spatialtable (GeomCol1)
VALUES (Geometry::stgeomfromtext (' LINESTRING (100 100, 20 180, 180 180) ', 0));

INSERT into spatialtable (GeomCol1)
VALUES (Geometry::stgeomfromtext (' POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0) ', 0));
GO

INSERT into spatialtable (GeomCol1)
VALUES (Geometry::stgeomfromtext (' LINESTRING (116.387112 39.920977,116.385243 39.913063,116.394226 39.917988,116.401772 39.921364,116.41248 39.927893,116.387112 39.920977) ', 4326)

Database db = Databasefactory.createdatabase ();
DbCommand dbcmd = db. Getsqlstringcommand ("Select GeomCol1 from spatialtable WHERE id=4");
using (IDataReader reader = db. ExecuteReader (dbcmd))
{
if (reader. Read ())
{
SqlGeometry o = reader[0] as SqlGeometry;

}
}

SqlGeometry Geo = Sqlgeometry.parse ("POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0)");
DataSet ds = db. ExecuteDataset (dbcmd);

SqlGeometry Geo = ds. Tables[0]. ROWS[0][1] as SqlGeometry;

SqlDouble area = Geo. Starea ();

dbcmd = db. Getsqlstringcommand (@ "INSERT into spatialtable (GeomCol1) VALUES (@p)");


Sqlgeometrybuilder sb = new Sqlgeometrybuilder ();
Sb. Setsrid (4326);
Sb. Begingeometry (Opengisgeometrytype.polygon);
Sb. BeginFigure (0, 0);
Sb. AddLine (150, 0);
Sb. AddLine (150, 150);
Sb. AddLine (0, 150);
Sb. AddLine (0, 0);
Sb. Endfigure ();
Sb. Endgeometry ();

dbcmd = db. Getsqlstringcommand (String. Format ("INSERT into Spatialtable" (GeomCol1) VALUES (Geometry::stgeomfromtext (' {0} ', {1})) "
, SB. Constructedgeometry.tostring (), 4326));
Db. Addinparameter (dbcmd, "@p", Dbtype.binary, sb.) Constructedgeometry.stasbinary (). Buffer);

int cnt = db. ExecuteNonQuery (dbcmd);

Get the system's coordinates (in SQL Server) from this sql: Select * from Sys.spatial_reference_systems

SQL Server Geometry Type action Note

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.