C.5. Step 4: Using Your Find Nearby Stored Procedure Using C#Now that you have the SQL Server implementation of the FindNearby ready, you can use it from your applications using the following C# code: public void FindNearbyBusinessEntities(double latitude, double longitude, double distance) { //Assume miles distance units for now //Change to 1 if you want Kilometers int units = 0; //Create SQL command //import System.Data.Sql namespace if you haven't already SqlCommand cmd = new SqlCommand("FindNearby"); //Assign input values to the sql command cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@CenterLat", latitude); cmd.Parameters.Add("@CenterLon", longitude); cmd.Parameters.Add("@SearchDistance", distance); cmd.Parameters.Add("@Units", units); //Define a connection SqlConnection sqlConn = null; try { //Open the connection sqlConn = new SqlConnection("your sql connection string"); // If the connection is closed, open it if(sqlConn.State == ConnectionState.Closed) sqlConn.Open( ); if(sqlConn.State == ConnectionState.Open) { cmd.Connection = sqlConn; SqlDataReader dreader = cmd.ExecuteReader( ); if(dreader != null) { while(dreader.Read( )) { //Get id of the business entity int id=0; if(!dreader.IsDBNull(0)) id = (int)dreader[0]; //Get name of the business entity string name = string.Empty; if(!dreader.IsDBNull(1)) name = (string)dreader[1]; //Do something userful with your entity such as //creating a puhspin object to render on map or //something . . . } } } } catch(Exception ex) { //Handle your exception processing here } finally { if(sqlConn != null) { if(sqlConn.State == ConnectionState.Open) { //Close sql connection sqlConn.Close( ); } } } } Now you have your own FindNearby capability that can be used in conjunction with other MapPoint Web Service features, such as rendering nearby business entities. |