public int ID(decimal jingdu,decimal weidu)
{
int i = 0;
string strconn = "Data Source=ANDY;Initial Catalog=GUIDE;Integrated Security=True";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand comm = new SqlCommand("", conn);
comm.CommandText = "select ID from andy where square(longitude-@jing)+square(latitude-@wei)=(select min((longitude-@jing)+square(latitude-@wei)) from andy)";
comm.Parameters.Add("jing",SqlDbType.Decimal);
comm.Parameters.Add("wei", SqlDbType.Decimal);
comm.Parameters["jing"].Value = jingdu;
comm.Parameters["wei"].Value = weidu;
conn.Open();
SqlDataReader result = comm.ExecuteReader();
if (result.Read() ==true)
{
i = (int)result[0];
}
conn.Close();
return i;
}
我的目的是接收到一个经纬度信息之后,与数据库里的经纬度信息比较后,返回与接收经纬度最接近的经纬度的ID号,可是运行之后,只能接收与数据库里相同的经纬度信息,如果接收到的信息数据库里不存在,就会返回初始值0,这是为什么??
{
int i = 0;
string strconn = "Data Source=ANDY;Initial Catalog=GUIDE;Integrated Security=True";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand comm = new SqlCommand("", conn);
comm.CommandText = "select ID from andy where square(longitude-@jing)+square(latitude-@wei)=(select min((longitude-@jing)+square(latitude-@wei)) from andy)";
comm.Parameters.Add("jing",SqlDbType.Decimal);
comm.Parameters.Add("wei", SqlDbType.Decimal);
comm.Parameters["jing"].Value = jingdu;
comm.Parameters["wei"].Value = weidu;
conn.Open();
SqlDataReader result = comm.ExecuteReader();
if (result.Read() ==true)
{
i = (int)result[0];
}
conn.Close();
return i;
}
我的目的是接收到一个经纬度信息之后,与数据库里的经纬度信息比较后,返回与接收经纬度最接近的经纬度的ID号,可是运行之后,只能接收与数据库里相同的经纬度信息,如果接收到的信息数据库里不存在,就会返回初始值0,这是为什么??
用这个吧.
id可能返回多个,举个简单的,如果有2个点相对 (@jing,@wei)对称的话.