I've tried everything I can think of and this simple C# code is not working. The result is that the parameters are NOT getting substituted so the query results in no data.
string query = @"select TOP 20 [ID], [NAME], LATITUDE, LONGITUDE, ADDRESS, FEATURES, [DATE UPDATED],
ABS(ABS(LATITUDE)- @Lat1 ) + ABS(ABS(LONGITUDE)- @Lng1 ) as diff
from Facility f
where (ABS(ABS(LATITUDE)- @Lat2 ) + ABS(ABS(LONGITUDE)- @Lng2 ) < 2)
order by 8";
using (SqlConnection conn = new SqlConnection())
{
// Create the connectionString
// Trusted_Connection is used to denote the connection uses Windows Authentication
conn.ConnectionString = "Server=ERICS_TOSHIBA\\WEAVSQL;Database=WellBe;Trusted_Connection=true";
conn.Open();
// Create the command
SqlCommand command = new SqlCommand(query, conn);
// Add the parameters.
command.Parameters.Add(new SqlParameter("@Lat1", SqlDbType.Float));
command.Parameters["@Lat1"].Value = dHomeLat;
command.Parameters.Add(new SqlParameter("@Lat2", SqlDbType.Float));
command.Parameters["@Lat2"].Value = dHomeLat;
command.Parameters.Add(new SqlParameter("@Lng1", SqlDbType.Float));
command.Parameters["@Lng1"].Value = dHomeLng;
command.Parameters.Add(new SqlParameter("@Lng2", SqlDbType.Float));
command.Parameters["@Lng2"].Value = dHomeLng;
Console.WriteLine("QUERY IS: \n\n {0}", command.CommandText);
}
The output of this WriteLine is this:
select TOP 20 [ID], [NAME], LATITUDE, LONGITUDE, ADDRESS, FEATURES, [DATE UPDATED],
ABS(ABS(LATITUDE)- @Lat1 ) + ABS(ABS(LONGITUDE)- @Lng1 ) as diff
from Facility f
where (ABS(ABS(LATITUDE)- @Lat2 ) + ABS(ABS(LONGITUDE)- @Lng2 ) < 2)
order by 8
Why won't the parameter values substitute? I've tried about 6 different versions of the syntax. Not all the code is listed above. There are valid values for all variables including the dHomeLat and dHomeLng.