Editing Spatial and Attribute Data


 

AspMap supports editing of data stored in spatial databases.

 

 

The following SQL Server table will be used to demonstrate editing of data.

NAME            TYPE
--------------------------------------
ID              int IDENTITY(1,1)
CITY_NAME       nvarchar(255)
STATE_NAME      nvarchar(255)
POPULATION      bigint
GEOM            geometry

The GEOM field stores shapes in the Geometry data format. The ID field is an auto-incremented primary key field.

 

For the purpose of this topic, the Layer.Open method will be used to open the table as layer. The table can also be added to a Map control as described here.
 

Create a SqlServerLayer object and pass the connection string, table name and GEOM field to its constructor. The FieldList property specifies the attribute fields which will be edited.

Imports AspMap
Imports AspMap.Data

Dim connectionString As String = "Data Source=SERVER\\SQLEXPRESS;Initial Catalog=DB;Integrated Security=SSPI;"
Dim tableName As String = "cities"

Dim sqlLayer As SqlServerLayer 

sqlLayer = New SqlServerLayer(connectionString, tableName, "GEOM")
sqlLayer.FieldList = "ID, CITY_NAME, STATE_NAME, POPULATION"
sqlLayer.DataType = SqlServerDataType.Geometry

Dim layer As Layer = Layer.Open(sqlLayer)
Adding records

Open the table as a layer as described above. Call the NewRecord method of the Layer class to add a new record as a Recordset object. The Recordset allows you to change the values of the CITY_NAME or POPULATION fields. The value of the spatial GEOM field can be set via the Shape property or SetShape method.

Dim recset as Recordset = layer.NewRecord()
recset["CITY_NAME"] = "New City"
recset["STATE_NAME"] = "Texas"
recset["POPULATION"] = 10500
recset.SetShape( new AspMap.Point(-98.17, 26.88) )

Call the Update method to save the new record in the database:

recset.Update()
Editing records

Open the table as a layer as described above. A database table must have a primary key field, which will be automatically used by AspMap to update records. The table above has the auto-incremented ID field of type IDENTITY. The key field must be specified in the FieldList property. If the table does not have a primary key field, an exception will be thrown.

 

The SearchExpression method will be used to find a record by city and state names. The expression will be sent directly to the database by setting the EnablePassthroughQuery property to True.

Dim recset as Recordset 
layer.EnablePassthroughQuery = True
recset = layer.SearchExpression("CITY_NAME = 'New City' AND STATE_NAME = 'Texas'")
If recset.EOF Then Return

The POPULATION and GEOM fields will be updated to reflect the growing of the city.

recset["POPULATION"] = 20000
recset.SetShape( new AspMap.Point(-98.20, 26.91) )

Call the Update method to save the record in the database:

recset.Update()
Removing records

Open the table as a layer as described above. A database table must have a primary key field, which will be automatically used by AspMap to delete records. The table above has the auto-incremented ID field of type IDENTITY. The key field must be specified in the FieldList property. If the table does not have a primary key field, an exception will be thrown.

 

The SearchExpression method will be used to find a record by its ID. The expression will be sent directly to the database by setting the EnablePassthroughQuery property to True.

Dim recset as Recordset 
layer.EnablePassthroughQuery = True
recset = layer.SearchExpression("ID = 30020")
If recset.EOF Then Return

Call the Delete method to remove the current record:

recset.Delete()

 

© 2018 VDS Technologies
Converted from CHM to HTML with chm2web Standard 2.85 (unicode)