I am using this code to select users from table in MySql,after i select i calculate the distance between the users and add only the users that are in 10KM from a gps location.
string commandLine = "SELECT * FROM Users;";
using (MySqlConnection connect = new MySqlConnection(connectionStringMySql))
using (MySqlCommand cmd = new MySqlCommand(commandLine, connect))
{
connect.Open();
using (MySqlDataReader msdr = cmd.ExecuteReader())
{
ArrayList array = new ArrayList();
while (msdr.Read())
{
double lon2 = msdr.GetDouble(19);
double lat2 = msdr.GetDouble(20);
double k = this.GetDistance(lat, lon, lat2, lon2);
//k is the distance
if (k <= 10)
{
//Add item to array
}
}
}
}
And i want to know if there is a possible to do this calculate with the Sql command instead in the code, because every time i get all the users from a table.
Edit
This is the GetDistance method:
private double GetDistance(double lat,double lon,double lat2,double lon2)
{
double ee = (3.1415926538 * lat / 180);
double f = (3.1415926538 * lon / 180);
double g = (3.1415926538 * lat2 / 180);
double h = (3.1415926538 * lon2 / 180);
double r = (Math.Cos(ee) * Math.Cos(g) * Math.Cos(f) * Math.Cos(h) + Math.Cos(ee) * Math.Sin(f) * Math.Cos(g) * Math.Sin(h) + Math.Sin(ee) * Math.Sin(g));
double j = (Math.Acos(r));
double k = (6371 * j);
return k;
}