SQL Server varchar(max)和varchar不兼容

时间:2016-06-19 01:20:07

标签: c# sql-server asp-classic varchar

我有一个大问题..

我用这个来进行测量地址(通过文本框插入)的距离(纬度和经度)的研究,如果距离是< 0.86 ...

此外,我还使用offset来执行分页。

public static List<Galleria> GetRistorante(int page, string key, string lon)
{
    Galleria Galleria = new Galleria(); 
    string im = string.Empty;

    int items = 10;
    int offset = (page - 1) * items;

    List<Galleria> lst = new List<Galleria>();

    using (SqlConnection sqlConn = new SqlConnection(DB.GetConnectionString()))
    {
        using (SqlCommand sqlComm = new SqlCommand())
        {
            sqlConn.Open();
            sqlComm.Connection = sqlConn;

            sqlComm.CommandText = @"SELECT R.* , C.* FROM dbo.Ristorante R INNER JOIN CategoriaRistorante C ON C.Id = R.CategoriaRistoranteId                                         
            HAVING (SQRT(POWER(69.1 * (R.lat - @key), 2) + POWER(69.1 * (@lon - R.lon) * COS(R.lat / 57.3), 2)) ) < 0.8699197
            ORDER BY R.Id OFFSET @offset ROWS FETCH NEXT @items ROWS ONLY";

            SqlParameter s1 = new SqlParameter();
            s1.SqlDbType = System.Data.SqlDbType.Int;
            s1.ParameterName = "@offset";
            s1.Value = offset;

            sqlComm.Parameters.Add(s1);

            SqlParameter s2 = new SqlParameter();
            s2.SqlDbType = System.Data.SqlDbType.Int;
            s2.ParameterName = "@items";
            s2.Value = items;

            sqlComm.Parameters.Add(s2);

            SqlParameter s3 = new SqlParameter();
            s3.SqlDbType = System.Data.SqlDbType.VarChar;
            s3.ParameterName = "@key";
            s3.Value = string.Format("%{0}%", Convert.ToDouble(key));

            sqlComm.Parameters.Add(s3);

            SqlParameter s4 = new SqlParameter();
            s4.SqlDbType = System.Data.SqlDbType.VarChar;
            s4.ParameterName = "@lon";
            s4.Value = string.Format("%{0}%", Convert.ToDouble(lon));

            sqlComm.Parameters.Add(s4);

            SqlDataReader reader = sqlComm.ExecuteReader();

            while (reader.Read())
            {
                Galleria = new Galleria();
                Galleria.Id = Convert.ToInt32(reader["Id"]);
                Galleria.Nome = reader["Nome"].ToString();
                Galleria.Info = reader["Info"].ToString();
                Galleria.Immagine = reader["Immagine"].ToString();
                Galleria.Telefono = reader["Telefono"].ToString();
                Galleria.Cap = Convert.ToInt32(reader["Cap"]);           
                Galleria.Via = reader["Via"].ToString();
                Galleria.Citta = reader["Citta"].ToString();

                lst.Add(Galleria);
            }

            return lst;
        }
    }
}

现在,当我尝试这种方法时,我收到了这个错误:

enter image description here

最初查询有效,但是当我添加&#34;有&#34;它给了我这个错误。

P.S。数据库中的纬度和经度为varchar(max)

1 个答案:

答案 0 :(得分:0)

尝试施放它。

HAVING (SQRT(POWER(69.1 * (CAST(R.lat AS DECIMAL(9,6)) - @key), 2) + POWER(69.1 * (@lon - CAST(R.lon AS DECIMAL(9,6))) * COS(CAST(R.lat AS DECIMAL(9,6)) / 57.3), 2)) ) < 0.8699197

然而,正如上面评论中提到的,我建议使用十进制字段本身,因为这个演员阵容将会受到影响。