wpf-devexpress-dot-xpf-dot-map-dot-sqlgeometrydataadapter.md
Gets or sets the connection string to a SQL database.
Namespace : DevExpress.Xpf.Map
Assembly : DevExpress.Xpf.Map.v25.2.dll
NuGet Package : DevExpress.Wpf.Map
public string ConnectionString { get; set; }
Public Property ConnectionString As String
| Type | Description |
|---|---|
| String |
A String value.
|
This example shows how to load data from a SQL geometry source and save changes made to map shapes to the source.
Follow the steps below to load data from a SQL geometry data source:
SqlGeometryDataAdapter.ConnectionString, SqlGeometryDataAdapter.SqlText and SqlGeometryDataAdapter.SpatialDataMember properties.Note that all field values loaded from the database (except for the SpatialDataMember field) are stored as attributes for each generated SqlGeometryItem object.
In this example, you can enable the Map Editor‘s Edit mode to relocate map shape points. To save changes made to shapes, call corresponding SQL commands in the MapEditor.MapItemEdited event handler.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"
x:Class="SqlGeometryDataAdapterExample.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<dxm:MapControl x:Name="mapControl">
<dxm:MapControl.MapEditor>
<dxm:MapEditor MapItemEdited="MapEditor_MapItemEdited"/>
</dxm:MapControl.MapEditor>
<dxm:VectorLayer DataLoaded="VectorLayer_DataLoaded">
<dxm:VectorLayer.ShapeTitleOptions>
<dxm:ShapeTitleOptions Pattern="{}{TextCol}"
Visible="True"/>
</dxm:VectorLayer.ShapeTitleOptions>
<dxm:SqlGeometryDataAdapter SqlText = "SELECT [id],[GeomCol1],[TextCol] FROM [dbo].[DemoTable]"
SpatialDataMember = "GeomCol1" ConnectionString="{Binding}"/>
</dxm:VectorLayer>
</dxm:MapControl>
</Grid>
</Window>
using DevExpress.Xpf.Map;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
namespace SqlGeometryDataAdapterExample {
public partial class MainWindow : Window {
const string filePath = "..\\..\\Data\\SQLG.mdf";
static string fullFilePath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath));
public string ConnectionString { get; } = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFileName=" + fullFilePath + ";Database=SqlGeometryDB;Integrated Security=True;MultipleActiveResultSets=True";
public MainWindow() {
InitializeComponent();
this.DataContext = ConnectionString;
}
private void MapEditor_MapItemEdited(object sender, DevExpress.Xpf.Map.MapItemEditedEventArgs e) {
foreach (MapPath path in e.Items) {
int id = Convert.ToInt32(path.Attributes["id"].Value);
string modified = path.ExportToWkt().ToString();
path.Attributes["TextCol"].Value = "Australia" + " " + DateTime.Now.Second.ToString();
using (SqlConnection cn = new SqlConnection() { ConnectionString = this.ConnectionString }) {
cn.Open();
// For more information about SRID parameters, see https://docs.microsoft.com/en-us/sql/t-sql/spatial-geography/stsrid-geography-data-type?view=sql-server-ver15
SqlCommand updatecmd = new SqlCommand("UPDATE DemoTable SET GeomCol1 = geometry::STGeomFromText('" + modified + "', 4326 ) WHERE id = " + id.ToString(), cn);
updatecmd.ExecuteNonQuery();
SqlCommand updateattr = new SqlCommand("UPDATE DemoTable SET TextCol = '" + path.Attributes["TextCol"].Value.ToString() + "' WHERE id =" + id.ToString(), cn);
updateattr.ExecuteNonQuery();
}
}
}
private void VectorLayer_DataLoaded(object sender, DataLoadedEventArgs e) {
mapControl.ZoomToFitLayerItems();
}
}
}
Imports DevExpress.Xpf.Map
Imports System
Imports System.Data.SqlClient
Imports System.IO
Imports System.Windows
Namespace SqlGeometryDataAdapterExample
Partial Public Class MainWindow
Inherits Window
Private Const filePath As String = "..\..\Data\SQLG.mdf"
Private Shared fullFilePath As String = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath))
Public ReadOnly Property ConnectionString() As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFileName=" & fullFilePath & ";Database=SqlGeometryDB;Integrated Security=True;MultipleActiveResultSets=True"
Public Sub New()
InitializeComponent()
Me.DataContext = ConnectionString
End Sub
Private Sub MapEditor_MapItemEdited(ByVal sender As Object, ByVal e As DevExpress.Xpf.Map.MapItemEditedEventArgs)
For Each path As MapPath In e.Items
Dim id As Integer = Convert.ToInt32(path.Attributes("id").Value)
Dim modified As String = path.ExportToWkt().ToString()
path.Attributes("TextCol").Value = "Australia" & " " & DateTime.Now.Second.ToString()
Using cn As New SqlConnection() With {.ConnectionString = Me.ConnectionString}
cn.Open()
' For more information about SRID parameters, see https://docs.microsoft.com/en-us/sql/t-sql/spatial-geography/stsrid-geography-data-type?view=sql-server-ver15
Dim updatecmd As New SqlCommand("UPDATE DemoTable SET GeomCol1 = geometry::STGeomFromText('" & modified & "', 4326 ) WHERE id = " & id.ToString(), cn)
updatecmd.ExecuteNonQuery()
Dim updateattr As New SqlCommand("UPDATE DemoTable SET TextCol = '" & path.Attributes("TextCol").Value.ToString() & "' WHERE id =" & id.ToString(), cn)
updateattr.ExecuteNonQuery()
End Using
Next path
End Sub
Private Sub VectorLayer_DataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
mapControl.ZoomToFitLayerItems()
End Sub
End Class
End Namespace
See Also