日期:2014-05-19  浏览次数:20740 次

这个程序错到哪了,请高手解答
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,这是为什么??

------解决方案--------------------
你的SQL語句是用相等作判斷條件的,當然不可能返回最接近的值
------解决方案--------------------
select ID from andy a where not exists (select * from andy b where square(b.longitude-@jing) + square(b.latitude-@wei) < (a.longitude-@jing) + square(a.latitude-@wei)) and a.longitude <> @jing and a.latitude <> @wei
用这个吧.
id可能返回多个,举个简单的,如果有2个点相对 (@jing,@wei)对称的话.