Before You Begin
In order to load geo-spatial data stored in a database, first you will have to convert the Shapefile to SQL database compliant format using third party utilities. Please refer to the About Shape to SQL Database Conversion Utilities topic for more information. In addition you need to have access to a remote SQL database server that contains the converted geo-spatial data.
What You Will Accomplish
Using a xamWebMap™ control’s MapLayer.Reader property and SqlShapeReader object, you will load geo-spatial data that is stored in a remote SQL Server 2008 database.

Follow These Steps
- Create a Microsoft® Silverlight® project with hosting in a new Web site.
- Go to the Host Web site project (the project name ending with .Web)
- Add a New Silverlight -enabled WCF Service and name it SqlDbService.svc

- Add the following namespaces in the SqlDbService class:
In Visual Basic:
ImportsSystem.Data.SqlClientImportsSystem.Collections.GenericImportsSystem.TextIn C#:
usingSystem.Collections.Generic;usingSystem.Data.SqlClient;usingSystem.Text; - Add the following method in the SqlDbService class
In Visual Basic:
<OperationContract()> _PublicFunctionGetData(ByValconnectionStringAsString,ByValcommandTextAsString)AsIEnumerable(Of Dictionary(OfString,String))DimlistAsNewList(Of Dictionary(OfString,String))()DimsqlConnectionAsNewSqlConnection() sqlConnection.ConnectionString = connectionStringDimsqlCommandAsNewSqlCommand() sqlCommand.Connection = sqlConnection sqlCommand.CommandType = System.Data.CommandType.Text sqlCommand.CommandText = commandText sqlConnection.Open()DimreaderAsSqlDataReader = sqlCommand.ExecuteReader()Whilereader.Read()DimvalueDictionaryAsNewDictionary(OfString,String)()ForiAsInteger= 0Toreader.FieldCount - 1 valueDictionary.Add(reader.GetName(i), reader.GetValue(i).ToString())Nextlist.Add(valueDictionary)EndWhilesqlConnection.Close() sqlConnection.Dispose()ReturnlistEndFunctionIn C#:
[OperationContract]publicIEnumerable<Dictionary<string,string>> GetData(stringconnectionString,stringcommandText) { List<Dictionary<string,string>> list =newList<Dictionary<string,string>>(); SqlConnection sqlConnection =newSqlConnection(); sqlConnection.ConnectionString = connectionString; SqlCommand sqlCommand =newSqlCommand(); sqlCommand.Connection = sqlConnection; sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.CommandText = commandText; sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader();while(reader.Read()) { Dictionary<string,string> valueDictionary =newDictionary<string,string>();for(inti = 0; i < reader.FieldCount; i++) { valueDictionary.Add(reader.GetName(i), reader.GetValue(i).ToString()); } list.Add(valueDictionary); } sqlConnection.Close(); sqlConnection.Dispose();returnlist; } - Save the SqlDbService file. In the Project Explorer, right click on SqlService.svc, and select the View it in Browser menu item to ensure that the service works.
- Right click on the Silverlight Application project (the project name NOT ending with “.Web”) And Select the Add Service Reference menu item.
- Click the Discover button and wait until the SqlDbService is discovered

- Change the entry in Namespace textbox to "SqlDbServiceReference" and click the OK button
- In the Solution Explorer, add the following reference to the Silverlight Application project:
- Infragistics.Silverlight.DataVisualization.v9.2.dll
- Infragistics.Silverlight.DataVisualization.Controls.v9.2.dll
- Infragistics.Silverlight.DataVisualization.Map.v9.2.dll
- Add the following namespace declarations for xamWebMap in the MainPage.xaml
In XAML:
xmlns:igMap="clr-namespace:Infragistics.Silverlight.Map;assembly=Infragistics.Silverlight.DataVisualization.Map.v9.2"xmlns:igCtl="clr-namespace:Infragistics.Silverlight.Controls;assembly=Infragistics.Silverlight.DataVisualization.Controls.v9.2" - Add the xamWebMap control to the page.
In XAML:
<igMap:XamWebMapx:Name="xamMap"><!-- TODO: Add Map Layer --></igMap:XamWebMap> - Create two MapLayer objects to display the country and road data.
In XAML:
<igMap:XamWebMap.Layers><!-- TODO: Add Map Country Layer --><igMap:MapLayerLayerName="CountryLayer"Fill="Gray"FillMode="None"><igMap:MapLayer.Reader><!-- NOTE: Replace SpatialDataColumn with your data column name --><igMap:SqlShapeReaderDataMapping="Data=SpatialDataColumn"/></igMap:MapLayer.Reader></igMap:MapLayer><!-- TODO: Add Map Road Layer --><igMap:MapLayerLayerName="RoadLayer"VisibleFromScale="0"Fill="Yellow"FillMode="None"><igMap:MapLayer.Reader><!-- NOTE: Replace SpatialDataColumn with your data column name --><igMap:SqlShapeReaderDataMapping="Data=SpatialColumn"/></igMap:MapLayer.Reader></igMap:MapLayer></igMap:XamWebMap.Layers> - In the MainPage.xaml.cs file, add the following namespace declarations
In Visual Basic:
ImportsInfragistics.Silverlight.MapImportsInfragistics.Silverlight.ControlsIn C#:
usingInfragistics.Silverlight.Map;usingInfragistics.Silverlight.Controls; - Add the following code to the constructor.
In Visual Basic:
PublicSubNew() InitializeComponent()DimconnStringAsString="Data Source=ServerName;Initial Catalog=DbName;Integrated Security=True"' - ServerName is name of server that contains Geospatial Database' - DbName is name of server that contains Geospatial DataDimcountryDataAsNewSqlDbServiceReference.SqlDbServiceClient()AddHandlercountryData.GetDataCompleted,AddressOfcountryData_GetDataCompleted countryData.GetDataAsync(connString,"select SpatialDataColumn from countryTable")' - SpatialDataColumn is name of column with Spatial Data' - CountryTable is name of table containg columns with Country Spatial DataDimroadDataAsNewSqlDbServiceReference.SqlDbServiceClient()AddHandlerroadData.GetDataCompleted,AddressOfroadData_GetDataCompleted roadData.GetDataAsync(connString,"select SpatialDataColumn from roadTable")' - SpatialDataColumn is name of column with Spatial Data' - RoadTable is name of table containg columns with Road Spatial DataEndSubIn C#:
publicMainPage() { InitializeComponent();stringconnString ="Data Source=ServerName;Initial Catalog=DbName;Integrated Security=True";// - ServerName is name of server that contains Spatial Database// - DatabaseName is name of server that contains Spatial DataSqlDbServiceReference.SqlDbServiceClient countryData =newSqlDbServiceReference.SqlDbServiceClient(); countryData.GetDataCompleted += countryData_GetDataCompleted; countryData.GetDataAsync(connString,"SELECT SpatialDataColumn FROM CountryTable");// - SpatialDataColumn is name of column with Spatial Data// - CountryTable is name of table containg columns with Country Spatial DataSqlDbServiceReference.SqlDbServiceClient roadData =newSqlDbServiceReference.SqlDbServiceClient(); roadData.GetDataCompleted += roadData_GetDataCompleted; roadData.GetDataAsync(connString,"SELECT SpatialDataColumn FROM RoadTable");// - SpatialDataColumn is name of column with Spatial Data// - RoadTable is name of table containg columns with Road Spatial Data} - Add the following two methods to handle the SqlServiceClient object’s GetDataCompleted events
In Visual Basic:
PrivateSubroadData_GetDataCompleted(ByValsenderAsObject,ByValeAsSqlDbServiceReference.GetDataCompletedEventArgs)DimsqlReaderAsSqlShapeReader =TryCast(xamMap.Layers("RoadLayer").Reader, SqlShapeReader)IfsqlReader IsNotNothingThensqlReader.DataSource = e.Result xamMap.Layers("RoadLayer").ImportAsync()EndIfEndSubPrivateSubcountryData_GetDataCompleted(ByValsenderAsObject,ByValeAsSqlDbServiceReference.GetDataCompletedEventArgs)DimsqlReaderAsSqlShapeReader =TryCast(xamMap.Layers("CountryLayer").Reader, SqlShapeReader)IfsqlReader IsNotNothingThensqlReader.DataSource = e.Result xamMap.Layers("CountryLayer").ImportAsync()EndIfEndSubIn C#:
voidroadData_GetDataCompleted(objectsender, SqlDbServiceReference.GetDataCompletedEventArgs e) { SqlShapeReader sqlReader = xamMap.Layers["RoadLayer"].ReaderasSqlShapeReader;if(sqlReader !=null) { sqlReader.DataSource = e.Result; xamMap.Layers["RoadLayer"].ImportAsync(); } }voidcountryData_GetDataCompleted(objectsender, SqlDbServiceReference.GetDataCompletedEventArgs e) { SqlShapeReader sqlReader = xamMap.Layers["CountryLayer"].ReaderasSqlShapeReader;if(sqlReader !=null) { sqlReader.DataSource = e.Result; xamMap.Layers["CountryLayer"].ImportAsync(); } } - Run the application. The xamWebMap control will load geo-spatial data stored in a remote database located on SQL Server 2008.
About Shape to SQL Database Conversion Utilities
