Lier SQLCE et MapPoint 2010

microraph Messages postés 24 Date d'inscription dimanche 21 octobre 2007 Statut Membre Dernière intervention 18 mai 2010 - 29 mars 2010 à 14:58
microraph Messages postés 24 Date d'inscription dimanche 21 octobre 2007 Statut Membre Dernière intervention 18 mai 2010 - 8 avril 2010 à 19:08
Bonjour à tous


Voilà en quelques lignes un problème que je rencontre toujours et encore avec MapPoint ; sauf que cette fois ci j'ai tout le code qu'il me faut mais je n'arrive pas à le mettre en forme de façon à ce que les deux bout se joigne.

Donc, au début pour MapPoint, pour l'importation de données on partait sur une base de données *.csv pour afficher ses données sur une carte pour là aucun soucis.

Désormais, les prof souhaitent que ce soit un peu plus dynamique en passant par une base de données locale SQLCE (dans Visual Studio 2008 en C#).

voici un code que j'ai trouvé dans un livre "Programming with MapPoint 2005 en C#" mais je n'arrive pas à envoyer ma requête sql vers la carte. Quelqu'un peut-il m'expliquer, me guider et me donner la solution ça serait super sympa. Merci à vous.

---- Code ---
/// <summary>
/// Import data from SQL database
/// </summary>
/// 


/// 


private void menuItem4_Click(object sender, System.EventArgs e)
{
//This sample uses System.Data namespace and standard NorthWind sample database that comes with SQL server

//See if there is an existing datasetwith the same name
object datasetName = "NorthWind Orders";
if(map.DataSets.Count > 0 && map.DataSets.get_Item(ref datasetName) != null)
//If so, delete it
map.DataSets.get_Item(ref datasetName).Delete();
//Now create a new dataset
MapPoint.DataSet dataset = map.DataSets.AddPushpinSet("NorthWind Orders");

//Define an index
object index = 1;

#region SQL data import code
//Define a connection string
string sqlConnectionString = "server=localhost;database=northwind;User Id=sqluser;Password=password";
//Define a connection object
System.Data.SqlClient.SqlConnection connection
= new System.Data.SqlClient.SqlConnection(sqlConnectionString);

//Define a SQL query
string sqlQueryString "SELECT CustomerID as ContactName, ShipName as CompanyName, ShipAddress as Address, ShipCity as City, ShipRegion As Region, ShipPostalCode as PostalCode, ShipCountry as Country FROM Orders WHERE ShipCountry 'USA'";

            

//Define a command object
System.Data.SqlClient.SqlCommand command =
new System.Data.SqlClient.SqlCommand(sqlQueryString, connection);

try
{
//Open the connection
connection.Open();
//Get a sql reader from the query
System.Data.SqlClient.SqlDataReader sqlReader = 
command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if(sqlReader != null)
{
while(sqlReader.Read())
{
//Get the name of the customer
string customername  = sqlReader["ContactName"] as String;
//Get the company name
string companyname  = sqlReader["CompanyName"] as String;
//Get the address string
string address = sqlReader["Address"] + ", " + sqlReader["City"] + ", " + sqlReader["Region"] + ", " + sqlReader["PostalCode"] + " " + sqlReader["Country"];
//Find location
MapPoint.Location location = null;
try
{
//Find the address
MapPoint.FindResults findrs = map.FindResults(address);
//If no results found, skip to next record
if(findrs == null || findrs.Count <= 0)
throw new Exception(address);
//Get the location
location = findrs.get_Item(ref index) as MapPoint.Location;
//Create a pushpin
if(location != null)
{
MapPoint.Pushpin pushpin = map.AddPushpin(location, companyname);
//Assign the contact name
pushpin.Note = "Contact : " + customername;
//Move to the pushpin dataset
pushpin.MoveTo(dataset);
}
}
catch
{
//MessageBox.Show("Unable to find : " + address);
}
}

//Close the reader, this will automatically close the connection
//due to the command behavior setting during the ExecuteReader method
sqlReader.Close();
}
}
catch
{
//Do clean up
}
#endregion

//Now use the data for some business purposes
//Find out how many orders are shipping within 100 miles your warehouse in Redmond
MapPoint.Location warehouse = 
map.FindResults("1 Microsoft Way, Redmond, WA").get_Item(ref index) as MapPoint.Location;
//Query for records (orders) that fall within 100 miles of distance around this warehouse
MapPoint.Recordset orders = dataset.QueryCircle(warehouse, 100);

//Count the orders
int orderCount = 0;
if(orders != null)
{
orders.MoveFirst();
while(!orders.EOF)
{
orderCount ++;
orders.MoveNext();
}
}

MessageBox.Show( orderCount.ToString() + " out of " + dataset.RecordCount.ToString() + " orders are shipping from Redmond, WA warehouse");

}

1 réponse

microraph Messages postés 24 Date d'inscription dimanche 21 octobre 2007 Statut Membre Dernière intervention 18 mai 2010
8 avril 2010 à 19:08
up^^
0
Rejoignez-nous