Can anyone show me a step by step example on how to use Sharpmap to display MS SQL server spatial data using C# as the programming language?

Please note I have exploit some of the example online but nor seem to be displaying data from MS SQL server 2008.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SharpMap;
using SharpMap.Geometries;
using SharpMap.Layers;
using SharpMap.Data.Providers;

namespace sqlcnx
    public partial class Form1 : Form
// we start with the connection string
       static string connstr = "user id=Youssef;password='';"+ 
//user id = name of your PC and because I use windows authentification I don't need passowrd

//write your PC name + \\SQL (my PC is YOUSSEF)

            "database=test;connection timeout=10";
//my database name is 'test' , the connection timeout is the time in second that the proram will waite for the server to connect before generating an exeption

   SqlConnection myConnection = new SqlConnection(connstr);
        Map map;
        VectorLayer vly= new VectorLayer("vly", new SharpMap.Data.Providers.SqlServer2008(connstr, "quartier", "shape", null));

// 'quartier' is the name of the table in my database and 'shape' represente the geometry column in that table

        public Form1() 

        private void Form1_Load(object sender, EventArgs e)
            map = new Map(pictureBox1.Size);
// we give the map the pictureBox size, don't forget to add a pictureBox to your form in the designer ;)

            vly.Style.EnableOutline = true;
            vly.Style.Outline = Pens.Black;
            vly.Style.Fill = Brushes.Red;
//the style of our vectorlayer color contour ..etc

           map.Layers.Add(vly); // add layer to the map
           map.ZoomToExtents(); //zoom to extentto see all the map
           map.BackColor = Color.White;
           pictureBox1.Image = map.GetMap();
//affect the map to the pictureBox


2 Answers 2


You need to specify an object id column! If you don't have one on your table, you need to add one (UINT).

You need to check your connection string. Is your SQL-Server instance really "YOUSSEF-PC\SQL"?
You can test if your connection works by invoking

System.Diagonstics.Debug.Assert(vly.GetFeatureCount() > 0);

Do you have geometry of geography objects in your table? If they are not geometry, you need to specify that in the constructor like

VectorLayer vly= new VectorLayer("vly", new SharpMap.Data.Providers.SqlServer2008(connstr, "quartier", "shape", "oid", SqlServerSpatialObjectType.Geography));

SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
        //vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("d:\\+PMF\\GIS\\states_ugl.shp", true);

        SharpMap.Data.Providers.SqlServer2008 d = new SharpMap.Data.Providers.SqlServer2008(connectionString, "view1","geom","ID",SharpMap.Data.Providers.SqlServerSpatialObjectType.Geometry,false  ,4326  );
       // mapBox1.Map.BackColor = Color.BlueViolet;
  • 2
    Welcome to GIS SE! Our protocols can take some getting used to but are you able to make this more of a step-by-step example answer like was asked for in the question? It is usually best to give code in answers some context about how it relates to answering the question.
    – PolyGeo
    Oct 16, 2013 at 10:23
  • I tried your sample code, i got this error message "Cannot use GeometryServiceProvider without an assigned IGeometryServices class", which lead to my next logical question how to i assign IGeometryServices or implement IGeometryService. Oct 18, 2013 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.