Passing an sql query

Mar 25, 2008 at 9:22 AM
Hi there,
For Rendering X and Y columns from a database as points
we arr using below.
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb"; //Connectionstring
string tablename = "pointsTable"; //Name of table in database
string oid = "ID"; //Name of object ID column - MUST be integer!
string xColumn = "X"; //Name of X coordinate column - MUST be double!
string yColumn = "Y"; //Name of Y coordinate column - MUST be double!

SharpMap.Map myMap = new SharpMap.Map(new System.Drawing.Size(500,250)); //Initialize map object
SharpMap.Layers.VectorLayer layAddresses = new SharpMap.Layers.VectorLayer("Addresses"); //Create layer
layAddresses.DataSource = new SharpMap.Providers.OleDbPoint(connStr, tablename, oid, xColumn, yColumn); //Set the datasource to the database
myMap.Layers.Add(layAddresses); //Add layer to map

myMap.ZoomToExtents(); //Zoom to extents
Image img = myMap.GetMap(); //Render map

how can we pass a sql query to oledbpoint instead of tablename.
to display the points on map according to the time intervals.
Developer
Mar 26, 2008 at 11:53 AM
If you look in the OleDbPoint class there is a method GetGeometriesInView which is called by the Render method on GetMap. If you put a breakpoint there while debugging you can get an idea of what is happening. It uses _defintionQuery in the WHERE statement. So, you can use the DefintionQuery to make a selection in your table. See code below.

string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb"; //Connectionstring
string tablename = "pointsTable"; //Name of table in database
string oid = "ID"; //Name of object ID column - MUST be integer!
string xColumn = "X"; //Name of X coordinate column - MUST be double!
string yColumn = "Y"; //Name of Y coordinate column - MUST be double!

SharpMap.Map myMap = new SharpMap.Map(new System.Drawing.Size(500,250)); //Initialize map object
SharpMap.Layers.VectorLayer layAddresses = new SharpMap.Layers.VectorLayer("Addresses"); //Create layer

//BEGIN EDIT
OleDbPoint oleDbPoint = new SharpMap.Providers.OleDbPoint(connStr, tablename, oid, xColumn, yColumn);
oleDbPoint.DefintionQuery = "ID = 0";
layAddresses.DataSource = oleDbPoint; //Set the datasource to the database
//END EDIT

myMap.Layers.Add(layAddresses); //Add layer to map

myMap.ZoomToExtents(); //Zoom to extents
Image img = myMap.GetMap(); //Render map