将函数的返回值赋给mssql查询的列

时间:2013-03-28 16:11:20

标签: c# asp.net sql sql-server-2008 sql-server-2012

如果有人知道我会非常感谢帮助我如何将功能的返回值指定为可在屏幕上打印的字段,这里是我的代码和更好的解释:

  SqlCommand myCommand = new SqlCommand("SELECT latitude,longitude,name FROM otherLocations WHERE dbo.DistanceInKm(" +myx + "," + myy + "," + "latitude" + "," + "longitude" + ") < 5", connection);
        SqlDataReader myReader = null;
        myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            Response.Write(myReader["latitude"].ToString() + "<br>");
            Response.Write(myReader["longitude"].ToString() + "<br>");
            Response.Write(myReader["name"].ToString() + "<br>");
        }

我想将dbo.DistanceInKm返回值分配给某个字段,以便我可以通过调用Response.Write(myReader["thatfield"].ToString() + "<br>");在页面上显示它 所以我想要做的是为表格中的每一行添加用distanceinkm函数定义的字段,这样我就可以在页面上打印它或者对它进行排序。

1 个答案:

答案 0 :(得分:1)

使用AS子句将该函数添加到您的选择列表中:

string query = @"SELECT latitude, longitude, name, 
     dbo.DistanceInKm(" +myx + "," + myy + "," + "latitude" + "," + "longitude" + @") < 5 
     as distance";

然后你可以像其他任何一栏一样从读者那里获得价值:

string distance = myReader["distance"].ToString();